NestJS Template
Fast-start NestJS 11 template with opinionated defaults for APIs: versioned routing, consistent response envelopes, Redis-backed caching, distributed throttling, and batteries-included logging/testing.
Highlights
- Production-ready Nest 11 + TypeScript with ESLint, Prettier, Jest (unit + e2e) and tsconfig-paths.
- API conventions baked in:
api/v1prefix, URI versioning, Helmet, graceful shutdown hooks, and a global exception filter. - Response serialization:
SerializeInterceptorenforces{ success, message, data }envelopes and can trim payloads via DTOs or be skipped per route. - Resilient caching + rate limiting: Redis-first CacheManager +
RedisServicehelper with automatic in-memory fallback, plus multi-window throttling backed by Redis storage. - Config-first:
ConfigModuleloads layered.envfiles, pipes values throughsrc/config/configuration.ts, and exposes typed helpers to the rest of the app. - Logging that scales: Winston + daily-rotate file transports, console-friendly formatting in dev, and env-controlled log levels.
Project layout
.
├── src
│ ├── app/ # Sample controller/service + DTOs
│ ├── common/
│ │ ├── filters/ # Global AllExceptionsFilter
│ │ ├── interceptors/ # Serialize decorator/interceptor
│ │ ├── redis/ # Cache + queue service with fallback
│ │ └── throttler/ # Multi-window rate limiter config
│ ├── config/ # Centralized configuration factory
│ ├── utils/logger/ # Winston logger factory
│ └── main.ts # App bootstrap (prefix, versioning, filters)
├── env.example # Copy to .env.* and customize per environment
├── dist/ # Build artifacts (generated)
├── test/ # e2e tests + Jest config
└── ...
Getting started
- Requirements: Node.js 20+, npm 10+, and (optionally) Docker for Redis.
- Environment variables:
cp env.example .envor create.env.development. Any of the following files will be picked up automatically (highest priority first):./src/config/env/.env.<NODE_ENV>./src/config/env/.env.local./.env.<NODE_ENV>./.env
- Install dependencies:
npm install - Run in watch mode:
npm run start:dev - Visit the sample endpoints:
GET http://localhost:3000/api/v1/→ Hello World envelopeGET http://localhost:3000/api/v1/healthGET http://localhost:3000/api/v1/demo/sample-user
Note: Redis is optional. If the app cannot reach the configured Redis host it will log a warning and fall back to the in-memory cache and throttler.
Environment variables
Use env.example as the source of truth. Copy it to .env, .env.development, or any file in src/config/env/ and adjust values per environment. Only variables that power the current template are included; add new ones when you implement matching features (DB, auth, third-party integrations, etc.).
| Group | Variables | Notes |
|---|---|---|
| Core | NODE_ENV, PORT, LOG_LEVEL |
NODE_ENV drives config resolution & logging style. |
| Redis | REDIS_HOST, REDIS_PORT, REDIS_PASSWORD, REDIS_DB, REDIS_KEY_PREFIX, REDIS_DEFAULT_TTL |
Leave REDIS_HOST blank to force in-memory cache mode. |
| Rate limiting | THROTTLER_* |
Controls the short/medium/long buckets (TTL in seconds + max requests). |
npm scripts
| Command | Description |
|---|---|
npm run start |
Start in the current NODE_ENV once. |
npm run start:dev |
Watch mode with live reload. |
npm run start:prod |
Run the compiled app (dist/main.js). |
npm run build |
Compile TypeScript to dist/. |
npm run lint |
ESLint across src, apps, libs, test. |
npm run format |
Prettier on src and test. |
npm run test |
Jest unit tests. |
npm run test:e2e |
e2e tests (Supertest + Jest). |
npm run test:cov |
Unit tests with coverage. |
Architecture notes
-
Global response handling
SerializeInterceptor(via the@Serialize()decorator or global usage) guarantees responses look like:{ "success": true, "message": "", "data": { ... } }Pass a DTO (
@Serialize(UserDto)) to trim payload fields viaclass-transformer, or omit the DTO to return the payload untouched. Use@SkipSerialize()for routes that already produce raw streams/buffers. -
AllExceptionsFilter
Every error funnels throughAllExceptionsFilter, so even unhandled errors become{ success: false, data: {} }payloads. Customize messages insrc/common/filters/exceptions.filter.ts. -
Configuration & secrets
ConfigModuleparses env files, merges them, and exposes typed sections (configService.get('redis'), etc.). This makes swapping providers (DB, email, SMS) straightforward. -
Caching & queues
RedisModuleregistersCacheModuleglobally. If Redis is reachable, it powers the cache and throttler; otherwise a memory store keeps the app running.RedisServicewraps common helpers likeget,set, and queue-like operations (enqueue,dequeue). -
Rate limiting
ThrottlerConfigModulewires three named buckets (short/medium/long) and stores counters in Redis when available. Adjust theTHROTTLER_*env vars to tune limits. -
Security & ops
main.tsenables Helmet, URI versioning (/api/v1/...), a global prefix, and graceful shutdown hooks for SIGINT/SIGTERM/unhandled rejections. -
Logging
src/utils/logger/logger.service.tsexposes a Winston logger with daily-rotating files (logs/), console-friendly dev output, and env-based log levels. ImportWinstonLoggerin modules that need advanced logging.
Redis setup (optional)
docker run --rm -p 6379:6379 --name hostell-redis redis:7-alpineUpdate REDIS_HOST=localhost (and password if needed). When Redis is down the template automatically falls back to memory, so development remains smooth.
Testing & quality
- Write unit tests next to source files (
*.spec.ts), then runnpm run test. - e2e tests live under
test/and hit the compiled app (npm run test:e2e). npm run lintplusnpm run formatkeep the codebase consistent.
Deployment
npm run build
NODE_ENV=production npm run start:prodUse process managers like PM2 or containers for long-running processes. Remember to provide the correct .env.production file (or real environment variables) on the target host.
License
Distributed under the MIT License. See LICENSE for details.