express-goodies
v1.3.4
Published
Common goodies for Chess Coders' Express + MongoDB starter package
Downloads
500
Readme
Express Goodies
Common utilities for Chess Coders' Express + MongoDB starter package.
Features
- Authentication and authorization middleware
- Loading and error state testing middleware
- reCAPTCHA validation middleware
- Yup schema validation middleware
- Soft delete Mongoose model and logic
- Audit trails Mongoose model and logic
- Pagination for every Mongoose model and Mongoose aggregation
- Rate limiting middleware
- Customizable middleware configuration via
site.config.js
Installation
Install the package via npm:
npm install express-goodies
Usage Example
// Import functions
const { myFunction } = require('express-goodies/functions');
// Import middleware
const { myMiddleware } = require('express-goodies/middleware');
// Import Mongoose models
const { myModel } = require('express-goodies/mongoose');
Configuration with site.config.js
To customize certain properties of the middleware provided by express-goodies
, you can create a site.config.js
file at the root of your project. This file allows you to override default settings without modifying the package code.
Example Structure of site.config.js
// site.config.js
module.exports = {
errorHandler: {
ignoredErrors: [
'Custom error message 1',
'Custom error message 2',
// Add more custom errors to ignore here
],
},
rateLimit: {
windowMs: 1 * 60 * 1000, // 1 minute
max: 100, // 100 requests per windowMs
// Add more configurations as needed
},
// Add configurations for other middleware as needed
};
How to Use Custom Configurations
1. Error Handler Middleware
The errorHandler
middleware handles errors across your application. You can specify additional errors to ignore in your logs by updating the ignoredErrors
array in site.config.js
.
Usage in your application:
// router.js
const express = require('express');
const middleware = require('express-goodies/middleware');
const router = express.Router();
// ... your routes ...
// Use the custom error handler as the last middleware
router.use(middleware.errorHandler);
module.exports = router;
Customization:
Add your custom errors to the ignoredErrors
array:
// site.config.js
module.exports = {
errorHandler: {
ignoredErrors: [
'ValidationError',
'UnauthorizedAccess',
// Add more errors to ignore
],
},
};
2. Rate Limiter Middleware
The speedLimiter
middleware helps control the rate of incoming requests to your application, preventing abuse and overloading. You can specify rate limit configurations in site.config.js
under the rateLimit
key.
Usage in your application:
// app.js (or your main Express configuration file)
const express = require('express');
const middleware = require('express-goodies/middleware');
const app = express();
// Apply the rate limiter middleware only to routes starting with '/public'
app.use('/public', middleware.speedLimiter);
// Define your public routes
app.get('/public/someRoute', (req, res) => {
res.send('This is a public route');
});
// Other routes and configurations...
// Use the custom error handler as the last middleware
app.use(middleware.errorHandler);
module.exports = app;
Customization:
Adjust the rate limiting settings in site.config.js
:
// site.config.js
module.exports = {
rateLimit: {
windowMs: 1 * 60 * 1000, // 1 minute
max: 100, // Limit each IP to 100 requests per windowMs
// Customize the response when the limit is exceeded
handler: (req, res, next, options) => {
res.status(options.statusCode).json({
message: 'Too many requests, please try again later.',
});
},
// You can add more options as needed
},
};
Notes
- Location: Place
site.config.js
at the root of your project. - Automatic Loading: The middleware functions in
express-goodies
automatically load configurations fromsite.config.js
. - No Additional Imports Needed: You don't need to import
site.config.js
manually in your application code.
Benefits of Using site.config.js
- Centralized Configuration: Manage all your middleware settings in one place.
- Ease of Maintenance: Update middleware settings without modifying the package code or searching through multiple files.
- Flexibility: Customize default behaviors to fit the specific needs of your application.