Skip to content

Node.js/Express Recipes

Your Express server works locally but the moment real traffic hits it, the error handling is nonexistent, the database connections leak, and nobody thought about rate limiting. These recipes fix that — each prompt produces production-hardened Node.js code that handles the real-world problems tutorials skip.

  • API scaffolding prompts with proper middleware, validation, and error handling
  • Database integration recipes with connection pooling and migrations
  • Authentication patterns with JWT refresh and RBAC
  • Testing and deployment recipes for production readiness

Recipe 1: Scaffold a Clean Architecture Express API

Section titled “Recipe 1: Scaffold a Clean Architecture Express API”

Scenario: You are starting a new service and need a project structure that scales beyond 10 routes without becoming unmaintainable.

Expected output: Project scaffold with 4-layer architecture, DI container, health check, graceful shutdown, and tests.


Recipe 2: Build a CRUD API with Validation and Error Handling

Section titled “Recipe 2: Build a CRUD API with Validation and Error Handling”

Scenario: You need a users API with proper input validation, consistent error responses, and typed request/response bodies.

Expected output: Route handlers, Zod schemas, validation middleware, error handler, catchAsync utility, and supertest tests.


Recipe 3: JWT Authentication with Refresh Token Rotation

Section titled “Recipe 3: JWT Authentication with Refresh Token Rotation”

Scenario: Your API needs authentication that does not force users to re-login every 15 minutes but also does not use long-lived tokens.

Expected output: Auth routes, JWT utilities, auth middleware, refresh token model, and security tests.


Recipe 4: Rate Limiting and Request Throttling

Section titled “Recipe 4: Rate Limiting and Request Throttling”

Scenario: Your public API gets hammered by scrapers and you have no protection against abuse.

Expected output: Rate limiter factory, Redis store integration, per-endpoint configs, response headers, and tests.


Recipe 5: Database Connection with Prisma and Transactions

Section titled “Recipe 5: Database Connection with Prisma and Transactions”

Scenario: Your database queries are scattered everywhere with no connection pooling, no transactions, and migrations run manually.

Expected output: Prisma schema, client singleton, transaction patterns, seed script, and transaction tests.


Recipe 6: Background Job Queue with BullMQ

Section titled “Recipe 6: Background Job Queue with BullMQ”

Scenario: Your API processes image uploads synchronously, blocking the response for 30 seconds. You need background processing.

Expected output: Queue setup, 3 processors, dashboard route, scheduled jobs, graceful shutdown, and tests.


Recipe 7: WebSocket Real-Time Communication

Section titled “Recipe 7: WebSocket Real-Time Communication”

Scenario: Your chat application polls the server every 2 seconds. You need real-time messaging.

Expected output: Socket.io setup, room handlers, presence system, message acknowledgment, and tests.


Recipe 8: Structured Logging with Request Tracing

Section titled “Recipe 8: Structured Logging with Request Tracing”

Scenario: Your production logs are console.log statements with no structure, no correlation, and no way to trace a request across services.

Expected output: Pino configuration, request middleware, child logger pattern, redaction rules, and tests.


Recipe 9: API Versioning and Documentation

Section titled “Recipe 9: API Versioning and Documentation”

Scenario: Your API is live with paying customers and you need to ship breaking changes without breaking their integrations.

Expected output: Versioned router, OpenAPI annotations, Swagger UI setup, deprecation middleware, and tests.


Recipe 10: File Upload with Streaming and Validation

Section titled “Recipe 10: File Upload with Streaming and Validation”

Scenario: Your file upload endpoint loads the entire file into memory, crashes on files over 100 MB, and accepts any file type.

Expected output: Upload route, S3 streaming, image processing, virus scanning, cleanup job, and tests.


Scenario: Your database gets crushed by repeated identical queries. The product listing page makes the same query 10,000 times per minute.

Expected output: CacheService, middleware, invalidation logic, warming script, monitoring endpoint, and tests.


Recipe 12: Docker and CI/CD for Production Deployment

Section titled “Recipe 12: Docker and CI/CD for Production Deployment”

Scenario: Your Node.js app deploys via SSH and manual npm install on the server. Time to containerize and automate.

Expected output: Dockerfile, compose files, GitHub Actions workflow, migration script, and rollback strategy.