Table of Contents
The Node.js Boilerplate is a project developed in TypeScript that uses concepts of Clean Architecture and Domain-Driven Design (DDD) to maintain a decoupled and domain-centered application structure.
In addition, various other design patterns are applied throughout the project to solve common software design problems encountered during the development process.
Finally, the use of Test-Driven Development (TDD) ensures several benefits for the code, such as better design, quality, greater development agility, and ease of maintenance and refactoring.
The project's goal is to demonstrate how it is possible to develop software in a decoupled manner. Thus, the example used in this boilerplate is extremely simple and can be divided into two main sets of features: Authentication and Persons.
The Authentication functionality allows Sign Up in the system and ensures that the access token is generated (Sign In) so that Persons resources, which are protected, can be accessed.
The Persons functionality allows an authenticated user to Fetch Persons in a paginated way and Get Person By Id.
Interaction with the API can be done in two ways, through the REST API or the GraphQL API.
This section describes the main libraries and tools used in the project, separated between development and production dependencies.
- @apollo/server - 4.11.2
- @as-integrations/fastify - 2.1.1
- @fastify/cors - 9.0.1
- @prisma/client - 6.1.0
- @scalar/fastify-api-reference - 1.25.90
- @scalar/themes - 0.9.58
- bcryptjs - 2.4.3
- fastify - 4.27.0
- graphql - 16.10.0
- jsonwebtoken - 9.0.2
- pg - 8.13.1
- zx - 8.2.4
- @commitlint/cli - 19.6.1
- @commitlint/config-conventional - 19.6.0
- @faker-js/faker - 9.3.0
- @semantic-release/changelog - 6.0.3
- @semantic-release/commit-analizer - 13.0.0
- @semantic-release/git - 10.0.1
- @semantic-release/github - 11.0.1
- @semantic-release/release-notes-generator - 14.0.2
- @types/bcryptjs - 2.4.6
- @types/jsonwebtoken - 9.0.7
- @types/node - 22.10.2
- @types/pg - 8.11.10
- @types/supertest - 6.0.2
- @vitest/coverage-v8 - 2.1.8
- @vitest/ui - 2.1.8
- coveralls - 3.1.1
- dotenv - 16.4.7
- eslint - 8.57.0
- eslint-config-love - "47.0.0",
- eslint-config-prettier - "9.1.0",
- eslint-plugin-import - "2.31.0",
- eslint-plugin-prettier - "5.2.1",
- eslint-plugin-promise - "6.1.1",
- eslint-plugin-vitest-globals - "1.5.0",
- husky - 9.1.7
- kleur - 4.1.5
- lint-staged - 15.2.11
- npm-check - 6.0.1
- openapi-types - 12.1.3
- prisma - 6.1.0
- semantic-release - 24.2.0
- supertest - 7.0.0
- tsup - 8.3.5
- tsx - 4.19.2
- typescript - 5.7.2
- vite-tsconfig-paths - 5.1.4
- vitest - 2.1.8
- vitest-mock-extended - 2.0.2
This section describes the main libraries and tools used in the project, separated between development and production dependencies.
In addition to Node.js, some other tools are required to run this project:
You can also install this toolkit for a better experience:
For security reasons, the .env
file is not versioned, meaning it is not sent to the GitHub repository.
So you can copy and paste the .env.example
file and rename it to .env
. Then, you need to configure the environment variables.
You must also setup a .env.test
to run the tests.
All variables can be configured according to your preferences.
I recommend that you leave the DATABASE_URL
variable as it is in the example, as it refers to the other database configuration environment variables.
The only point of attention is regarding JWT_PRIVATE_KEY
and JWT_PUBLIC_KEY
. The JWT adapter reads the base64 values of these two environment variables. That is, in this case, there is no reading of the .pem files to obtain the keys.
To generate a pair of keys on MacOS, you can use these commands:
# generate private key
openssl genpkey -algorithm RSA -out private-key.pem -pkeyopt rsa_keygen_bits:2048
# generate public key
openssl rsa -pubout -in private-key.pem -out public-test-key.pem
Now it is necessary to transform the .pem files into base64. To do this on MacOS, you can follow these steps:
# transform private key in base64
openssl base64 -in private-key.pem -out private-key.txt
# transform public key in base64
openssl base64 -in public-key.pem -out public-key.txt
Now, the values obtained in the .txt files are the private and public keys in base64. These are the values that you must set for JWT_PRIVATE_KEY
and JWT_PUBLIC_KEY
.
Install the dependencies with your favorite package manager (npm, yarn, or pnpm). In the example below, the default package manager for Node.js, npm, will be used:
npm install
The wizard is a simple CLI contained in the project, developed with Google ZX to assist developers with various tasks. To run it, you can use one of the following scripts:
# Without logs
npm run wizard
# With logs
npm run wizard:logs
Now just choose one of the options below and wait for the magic to happen!
To run unit tests (*.spec.ts):
npm run test
To run unit tests (*.spec.ts) in watch mode:
npm run test:watch
To run e2e tests (*.test.ts):
npm run test:e2e
To run e2e tests (*.test.ts) in watch mode:
npm run test:e2e:watch
To run all tests unit and e2e tests (*.spec.ts and *.test.ts)
npm run test:ci
If database container is running, you can run:
npm run start:dev
Or you can use wizard:
npm run wizard
And choose option 5!