2.1.0 • Published 5 months ago

express-goodies v2.1.0

Weekly downloads
-
License
MIT
Repository
-
Last release
5 months ago

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 from site.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.
2.1.0

5 months ago

2.0.3

5 months ago

2.0.2

6 months ago

2.0.1

6 months ago

2.0.0

7 months ago

1.3.4

9 months ago

1.2.0

10 months ago

1.3.3

9 months ago

1.3.2

9 months ago

1.3.1

9 months ago

1.3.0

10 months ago

1.2.1

10 months ago

1.1.1

1 year ago

1.1.0

1 year ago

1.0.7

1 year ago

1.0.6

1 year ago

1.0.5

1 year ago

1.0.3

2 years ago

1.0.2

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago

0.6.2

2 years ago

0.6.1

2 years ago

0.6.0

2 years ago

0.5.1

2 years ago

0.5.0

2 years ago

0.4.2

2 years ago

0.4.1

2 years ago

0.4.0

2 years ago

0.3.1

2 years ago

0.3.0

2 years ago

0.2.1

2 years ago

0.2.0

2 years ago

0.1.1

2 years ago

0.1.0

2 years ago