Nest Template is a comprehensive application development setup that covers various aspects, from environment setup to security, documentation, and more. This README will guide you through the setup and various features of the project.
- Node (v20.17.0 or higher)
- NPM (v10.9.0 or higher)
- Docker
- Prettier
- EsLint
(Hint: Install the extensions under .vscode/extensions.json
)
- Clone Nest Template into an empty folder.
- Configure
.env
:- Replace
APP_NAME
with your project name. - Generate a
JWT_SECRET
usingnode -e "console.log(require('crypto').randomBytes(256).toString('base64'));"
- Adjust
DATABASE_URL
. - Set
FRONTEND_URL
. - Configure MailJet settings.
- Replace
- Update
package.json
(name, version, etc.). - Modify
/client/index.html
(name). - Replace
/client/favicon.ico
. - Add mail templates.
- Install packages with
npm install
. - Start the database with
docker-compose up
. - Synchronize the database with
npx prisma migrate deploy
. - Seed the database with
npx prisma db seed
. - Run the development server with
npm run start:dev
.
Optional: 12. Open the official NestJS Documentation at https://docs.nestjs.com/
.
You can also use a local PostgreSQL Database.
- Configure
.env
with.env.development
. - Start Docker with
docker-compose up
.
Open Prisma Studio with npx prisma studio
on localhost:5555.
Learn more about JWT.
Every endpoint is secured by default. To disable this behavior, you can use the @Public
decorator on the controller.
To access the secured endpoints, you need an Authorization Bearer Token
in the request headers.
Use /auth/login
to post your email
and password
, and in return, you will get an accessToken and refreshToken.
The Access Token serves as the Authorization Bearer Token.
To refresh the Access Token, use the Refresh Token on /auth/refresh
with a POST request and body containing the refreshToken
.
Explore CASL.
In CASL, every schema is a Subject. Each Subject has multiple Actions (default CRUD methods and Manage, which represents all actions).
Users are granted access to various actions on a subject based on their role.
After a user registers or the endpoint /auth/email/email-verification
is called, an email is sent via an SMTP server to verify the user's email address.
You can edit the email template under src/mail/templates/*.hbs
, and configure frontend endpoints in src/config/endpoints.ts
.
To verify the user's email, call /auth/verify-email
.
Note: All endpoints that are not GET requests cannot be called until the email is verified.
If a user forgets their password, they can send themselves an email to reset it. The endpoint is at /auth/email/reset-password
.
You can edit the reset password template under src/mail/templates/*.hbs
, and configure frontend endpoints in src/config/endpoints.ts
.
To reset the password, send the token and the new password to the endpoint /auth/reset-password
.
Learn about Compression, which greatly reduces the size of the response body, increasing the speed of a web app.
Learn about Helmet, which helps protect your app from well-known web vulnerabilities by setting appropriate HTTP headers. Helmet is a collection of smaller middleware functions that set security-related HTTP headers.
Learn about CORS, a mechanism that allows resources to be requested from another domain. By default, only the Frontend URL in .env
can access the backend.
Learn about serving static files with Static Serve. This serves all files in the client
folder.
In the Prisma folder, you can write your own seeds with @faker-js/faker
.
Seed the database with npx prisma db seed
.
Learn about the custom logger service with winston. It prints to the console and to files in logs
.
In development, it logs all messages with log level Debug
, and in production, it uses log level Info
.
All HTTP requests are logged with a custom middleware.
BUG: When a Module, Service, or Controller is injected incorrectly, NestJS won't log it due to the custom Logger Service.
Health checks are created to ensure that external services (like the database) are available and can be accessed via /health
.
Global Throttler can be configured in .env
:
- THROTTLER_TTL: the number of seconds that each request will last in storage.
- THROTTLER_LIMIT: the maximum number of requests within the TTL limit.
Validation via DTO. Check available rules and transforms here.
Learn about Prisma and Nest Prisma.
Migrate the database with npx prisma migrate dev --name ${__name__}
. Replace ${__name__}
with the migration name.
Use Transactions with prisma.$transaction([ ... ])
.
Learn about Configuration.
Automatic Project Documentation with Compodoc.
Generate documentation for the current codebase with npm run doc:generate
. Access it at http://localhost:4201
with doc:serve
.
Automatic API Documentation with Swagger.
Access the API documentation at /api
(authorization is still required for secured endpoints).
No Tests yet :(
NestJS comes with a powerful CLI Tool.