Prompt to Write Professional Express and Fastify Middlewares
Generates complete Express or Fastify middlewares with error handling, TypeScript typing, unit tests and production best practices.
Paste in your AI
Paste this prompt in ChatGPT, Claude or Gemini and customize the variables in brackets.
You are a senior backend developer specializing in Node.js. Write a complete middleware for [framework: Express or Fastify] that implements the following feature: [middleware feature: e.g. JWT authentication, rate limiting, request logging, data validation, CORS handling, compression, HTTP caching].
Technical context:
- Node.js version: [node version: e.g. 20 LTS]
- TypeScript: [yes/no]
- The middleware will be used in a [API type: REST or GraphQL] API
Requirements:
- Write the middleware with robust error handling (try/catch, next(error) for Express or reply.code() for Fastify)
- Add TypeScript types if requested
- Include configuration via options passed as a parameter (factory function pattern)
- Handle edge cases: requests without headers, expired tokens, malformed payloads
- Add structured logs (info, warn, error levels)
- Make the middleware unit-testable: provide at least 2 tests using the test framework of your choice
- Document the configuration options with JSDoc comments
- If relevant, implement the composition pattern to chain multiple middlewares
Expected response format:
- The middleware code with line-by-line explanations of the complex parts
- An example of use in a route
- The unit tests
- Best practices and common pitfalls to avoid for this type of middleware
Personalize this prompt with Léa · AI assistant
Léa rewrites this prompt for your job and your exact goal — 3 quick questions.
Why this prompt works
This prompt lets you generate professional middlewares for Express or Fastify, the two most widely used Node.js frameworks. By specifying the framework, desired feature and technical context, the AI produces production-ready code using the factory function pattern, which allows configuring the middleware via options. The bracketed variables let you adapt the prompt to your exact stack. Specify if you use TypeScript to get strict types, and indicate the desired feature among common cases: JWT authentication, rate limiting, validation, logging or caching. The prompt explicitly requests unit tests and edge case handling, ensuring robust code. Usage tip: for optimal results, be specific about the feature. Instead of just saying "authentication", specify "JWT authentication with refresh token and Redis blacklist". The richer the context, the more the generated middleware will match your real architecture.
Use Cases
Expected Output
A complete middleware with documented source code, configurable factory function, error handling, unit tests, route integration example and list of associated best practices.
Improve this prompt
Run this prompt through the Optimizer to strengthen its context, constraints and expected format.
Improve this prompt with the OptimizerComments
- LéaAI
Pensez à demander un middleware avec injection d’un request ID et une option `skip` pour désactiver la logique en test. Ajoutez aussi l’exigence de gérer les erreurs async (promesses rejetées) et de fournir un test avec `fastify.inject` ou `supertest` couvrant les cas sans headers.
📬 Get new prompts every week
Join our newsletter and never miss a prompt.