exhandlers is a comprehensive collection of middleware and handler functions designed to streamline backend development in Express.js. It provides pre-built utilities for handling databases, WebSocket connections, AMQP connections, logging, and various other essential middleware functionalities.
By integrating exhandlers, developers can eliminate the need to write common handlers from scratch, thereby reducing development overhead and ensuring a cleaner, more maintainable codebase.
- Asynchronous Handler – Simplifies error handling in async functions.
- Authentication Handler – Manages authentication and authorization mechanisms.
- CORS Handler – Configures Cross-Origin Resource Sharing (CORS) policies.
- Error Handler – Centralized error handling middleware.
- Not Found Handler – Handles 404 (Not Found) errors.
- Logs Handler – Structured logging for application monitoring.
- Rate Limiting Handler – Protects APIs from excessive requests.
- Password Handler – Utility functions for password hashing and validation.
- Token Handler – Utility function for password hashing and validation.
- MongoDB Handler – Provides a streamlined connection and query interface for MongoDB.
- PostgreSQL Handler – Manages PostgreSQL database connections and queries.
- Redis Handler – Integrates Redis for caching and session management.
- Upload Handler – Manages file uploads efficiently.
- Socket Handler – Handles WebSocket connections for real-time data exchange.
- AMQP Handler – Facilitates message queue communication using AMQP (e.g., RabbitMQ).
To integrate exhandlers into your project, install the package using npm:
npm install exhandlersTo use exhandlers in your Express application, import the required handlers using require.
const {
asyncHandler,
errorHandler,
notFoundHandler,
corsHandler,
rateLimitHandler,
} = require("exhandlers");
const express = require("express");
const app = express();
// Apply CORS middleware
app.use(corsHandler());
// Apply rate limiting middleware
app.use(rateLimitHandler({ windowMs: 15 * 60 * 1000, limit: 100 })); // 100 requests per 15 minutes
// Define an asynchronous route using asyncHandler
app.get(
"/data",
asyncHandler(async (req, res) => {
const data = await fetchDataFromDB(); // Simulated database call
res.json({ success: true, data });
}),
);
// Handle 404 errors
app.use(notFoundHandler());
// Centralized error handling middleware
app.use(errorHandler());
// Start the server
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
console.log(`Server is running on port ${PORT}`);
});
// Sample function simulating database fetch
async function fetchDataFromDB() {
return { message: "Hello from the database!" };
}This setup ensures your Express server is optimized for security, error handling, and performance.
This project is licensed under the MIT License.