Best practices for Express.js Middleware Patterns
.cursorrules in your project root# Express.js Middleware Patterns ===================================== ## Core Principles ------------------ * **Separation of Concerns**: Each middleware function should have a single responsibility and not mix multiple, unrelated functionalities. * **Reusability**: Middleware functions should be designed to be reusable across different routes and applications. * **Readability**: Middleware functions should be easy to understand and follow a consistent naming convention. ## Code Style Guidelines ----------------------- ### Naming Conventions * **Middleware Function Names**: Use camelCase and include a descriptive suffix (e.g., `authenticateUser`, `validateRequestData`). * **Variable Names**: Use meaningful and descriptive variable names (e.g., `req`, `res`, `next`). ### Typing * **Type Definitions**: Use JSDoc-style comments to define the types of function parameters and return values. * **Type Checking**: Use a type checker like TypeScript to ensure type safety. ## Best Practices ----------------- ### Middleware Function Structure * **Keep it Simple**: Avoid complex logic and conditional statements within middleware functions. * **Error Handling**: Handle errors and exceptions properly, and pass them to the next error-handling middleware function using `next(err)`. * **Async/Await**: Use async/await syntax to handle asynchronous operations and avoid callback hell. ### Middleware Ordering * **Order Matters**: The order of middleware functions in the stack matters, as each function will be executed in sequence. * **Error-Handling Middleware**: Place error-handling middleware functions at the end of the stack to catch any errors that occur in previous middleware functions. ### Request and Response Objects * **Request Object**: Use the `req` object to access request data, such as query parameters, body, and headers. * **Response Object**: Use the `res` object to send responses, such as JSON data, redirects, and errors. ## Common Pitfalls to Avoid --------------------------- ### Overusing Middleware Functions * **Avoid Over-Engineering**: Don't create too many middleware functions, as this can lead to a complex and hard-to-maintain codebase. * **Keep it Focused**: Ensure each middleware function has a clear and specific purpose. ### Not Handling Errors Properly * **Error Handling**: Always handle errors and exceptions properly, and pass them to the next error-handling middleware function using `next(err)`. * **Don't Swallow Errors**: Avoid swallowing errors, as this can lead to unexpected behavior and make debugging difficult. ### Not Following Best Practices * **Follow Established Patterns**: Follow established middleware patterns and best practices to ensure consistency and maintainability. * **Keep it Simple and Consistent**: Avoid complex and inconsistent code, as this can lead to maintenance issues and make the codebase harder to understand.
Comprehensive Cursor rules for Next.js 14+ with App Router, including routing, layouts, and API patterns.
Cursor rules for TypeScript with strict type checking, advanced patterns, and best practices.
Cursor rules for Tailwind CSS development with responsive design, custom components, and dark mode.
Cursor
coding
AI coding rules customize how Cursor generates and refactors code for your project. Follow these steps to install Express.js Middleware Patterns.
.cursor/rules, for Windsurf use .windsurfrulesComprehensive Cursor rules for Next.js 14+ with App Router, including routing, layouts, and API patterns.
Cursor rules for TypeScript with strict type checking, advanced patterns, and best practices.
Cursor rules for Tailwind CSS development with responsive design, custom components, and dark mode.