NestJS REST API Best Practices That Scale
Module boundaries, DTO validation, guards, exception filters and configuration: the NestJS structure that stays clean as your API grows.
A pipeline that runs on every push catches broken code before it reaches users. GitHub Actions makes it almost free to set up.
Create .github/workflows/ci.yml:
name: CI
on:
push:
branches: [main]
pull_request:
jobs:
check:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 22
cache: npm
- run: npm ci
- run: npm run lint
- run: npx tsc --noEmit
- run: npm test --if-present
- run: npm run build
The cache: npm option reuses downloads between runs, cutting minutes to seconds.
Require the CI check to pass before merging a pull request. Nobody, including you, can then push broken code to production by accident.
Platforms like Vercel deploy on every push by themselves. For servers, add a job that runs after checks pass and uses a deploy key or an official action. Keep secrets in the repository settings, never in the workflow file.
Slow pipelines get ignored. Run independent jobs in parallel and cache aggressively.
Lint, type check, test, build, then deploy. Automate the boring checks so reviews can focus on design.
Module boundaries, DTO validation, guards, exception filters and configuration: the NestJS structure that stays clean as your API grows.
Schema design, migrations, relations, transactions and performance tips for using Prisma with PostgreSQL, MySQL or MongoDB.
Implement secure login with short-lived access tokens, rotating refresh tokens and httpOnly cookies, and avoid the common JWT mistakes.