P
💻DeveloppementIntermediateAll AIs

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

Adapt this prompt

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:

  1. Write the middleware with robust error handling (try/catch, next(error) for Express or reply.code() for Fastify)
  2. Add TypeScript types if requested
  3. Include configuration via options passed as a parameter (factory function pattern)
  4. Handle edge cases: requests without headers, expired tokens, malformed payloads
  5. Add structured logs (info, warn, error levels)
  6. Make the middleware unit-testable: provide at least 2 tests using the test framework of your choice
  7. Document the configuration options with JSDoc comments
  8. 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

Create a JWT authentication middleware with refresh token managementImplement a configurable per-route rate limiter with Redis storageDevelop a structured logging middleware compatible with an ELK stackWrite a request validation middleware based on a Zod or Joi schema

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 Optimizer

Comments

  • 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.

Go further

Similar Prompts

💻DeveloppementIntermediateAll AIs

Build an accessible form

Build WCAG-compliant forms

519
💻DeveloppementAdvancedAll AIs

Implement canary deployments

Roll out changes safely with canary

478
💻DeveloppementIntermediateAll AIs

Write a Helm chart

Package apps as Helm charts

521
💻DeveloppementAdvancedChatGPT

Legacy Code Refactoring

Safely and incrementally refactor legacy code following SOLID principles and modern best practices.

974