npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

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 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.