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.
Tu es un développeur backend senior spécialisé en Node.js. Écris un middleware complet pour [FRAMEWORK : Express ou Fastify] qui implémente la fonctionnalité suivante : [FONCTIONNALITÉ DU MIDDLEWARE : ex. authentification JWT, rate limiting, logging des requêtes, validation des données, gestion CORS, compression, cache HTTP].
Contexte technique :
- Version de Node.js : [VERSION_NODE : ex. 20 LTS]
- TypeScript : [OUI/NON]
- Le middleware sera utilisé dans une API [TYPE_API : REST ou GraphQL]
Exigences :
- Écris le middleware avec une gestion d'erreurs robuste (try/catch, next(error) pour Express ou reply.code() pour Fastify)
- Ajoute des types TypeScript si demandé
- Inclus la configuration via des options passées en paramètre (pattern factory function)
- Gère les cas limites : requêtes sans headers, tokens expirés, payloads malformés
- Ajoute des logs structurés (niveau info, warn, error)
- Rends le middleware testable unitairement : fournis au moins 2 tests avec le framework de test de ton choix
- Documente les options de configuration avec des commentaires JSDoc
- Si pertinent, implémente le pattern de composition pour chaîner plusieurs middlewares
Format de réponse attendu :
- Le code du middleware avec explications ligne par ligne des parties complexes
- Un exemple d'utilisation dans une route
- Les tests unitaires
- Les bonnes pratiques et pièges courants à éviter pour ce type de middleware
Personalize this prompt with Léa
Answer 3 questions and Léa tailors the prompt to your situation.
Why this prompt works
<p>This prompt lets you generate professional middlewares for <strong>Express</strong> or <strong>Fastify</strong>, 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.</p><p>The bracketed variables let you adapt the prompt to your exact stack. Specify if you use <strong>TypeScript</strong> to get strict types, and indicate the desired feature among common cases: JWT authentication, rate limiting, validation, logging or caching. The prompt explicitly requests <strong>unit tests</strong> and edge case handling, ensuring robust code.</p><p><strong>Usage tip</strong>: 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.</p>
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.
Learn more
Check the full skill on Prompt Guide to master this technique from A to Z.
View on Prompt GuideComments
Be the first to comment on this prompt.
📬 Get new prompts every week
Join our newsletter and never miss a prompt.
Similar Prompts
Implement property-based testing
Add property-based tests
Integration Testing for REST APIs
Test API endpoints under real-world conditions
Write Integration Tests for an API
Create complete API integration tests with database setup, authentication, CRUD, and end-to-end scenarios.
Implement Robust Error Handling in Node.js
Create a professional Node.js error handling architecture with custom error classes, centralized middleware, and monitoring.