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

@gasbuddy/meddleware

v2.0.0

Published

Configuration-based middleware registration for express.

Downloads

451

Readme

meddleware

Lead Maintainer: Jean-Charles Sisk

Build Status

Configuration-based middleware registration for express.

Note: meddleware >=1.0 is only compatible with express >=4.0. For express 3.x support, please use meddleware 0.1.x.

Usage

var http = require('http'),
    express = require('express'),
    meddleware = require('meddleware'),
    config = require('shush')('./config/middleware');

var app = express();
app.use(meddleware(config)); // or app.use('/foo', meddleware(config));
http.createServer(app).listen(8080);

Configuration

meddleware configuration consists of an object containing properties for each of the middleware to be registered.

{
    "favicon": {
        "enabled": true,
        "priority": 10,
        "module": "static-favicon"
    },

    "static": {
        "enabled": true,
        "priority": 20,
        "module": {
            "name": "serve-static",
            "arguments": [ "public" ]
        }
    },

    "custom": {
        "enabled": true,
        "priority": 30,
        "route": "/foo",
        "module": {
            "name": "./lib/middleware",
            "method": "customMiddleware",
            "arguments": [ "foo", { "bar": "baz" } ]
        }
    },

    "security": {
        "enabled": true,
        "priority": 40,
        "route": [ "/foo", "/bar" ],
        "module": {
            "name": "./lib/security",
            "arguments": [ { "maximum": true } ]
        }
    },

    "cookieParser": {
        "enabled": false,
        "priority": 50,
        "module": {
            "name": "cookie-parser",
            "arguments": [ "keyboard cat" ]
        }
    },

    "misc": {
        "priority": 60,
        "parallel": {
            "user": {
                "enabled": true,
                "module": {
                    "name": "the-module",
                    "arguments": []
                }
            },
        }
    }
}

Options

  • enabled (boolean) - Set to true to enable middleware, false to disable. Defaults to true.

  • priority (number) - The weight to give a particular piece of middleware when sorting for registration. Lower numbers are registered first, while higher numbers are registered later. If priority is not a number, this setting defaults to Number.MIN_VALUE.

  • module (object or string) - The name or definition of the module to load containing the middleware implementation. Can be an installed module or a path to a module file within your project/application.

    • name (string) - The name of the module or path to local module.

    • method (string, optional) - The method on the provided module upon which invocation will create the middleware function to register. If a factory method is not provided, it defaults to the name of the current middleware being processed, and finally back to the module itself.

    • arguments (array, optional) - An array of arguments to pass to the middleware factory.

  • route (string or array or regexp, optional) - An express route against which the middleware should be registered. Can be a string or a regular expression, or an array consisting of strings and regular expressions.

    Caveats
    1. If configuring meddleware with json files, you'll need to use something like shortstop with shortstop-regex to convert a string to RegExp.

    2. String paths will be automatically prefixed with any mountpath but regular expressions will not.

    var config = {
      myMiddleware: {
        module: './myMiddleware',
        route: '/foo'
      },
      otherMiddleware: {
        module: './otherMiddleware',
        route: /^\/bar$/i
      }
    }
    
    app.use('/baz', meddleware(config));
    
    // `myMiddleware` will run on `/baz/foo`
    // `otherMiddleware` will run on `/bar`
  • parallel (object, optional) - A meddleware configuration object containing middleware which should be executed in parallel, proceeding only when all have completed.

  • race (object, optional) - A meddleware configuration object containing middleware which should be executed in parallel, but will proceed when the first one completes.

  • fallback (object, optional) - A meddleware configuration object containing middleware which should be executed sequentially, proceeding upon first successfully resolved middleware, falling back in the event of a failure.

Express App Events

Along with registration, consumers can be notified of registration events. NOTE: These events are only triggered for the middleware that is registered via meddleware. All middleware events receive the following eventargs object:

{
   app: [object Object], // express app
   config: [object Object] // config object for the current middleware
}

There are 4 types of events one can subscribe to:

  • middleware:before - Subscribe to this event to be notified immediately before every middleware registration. The event handler will receive an eventargs object containing 2 properties: app being the express application against which the middleware was registered, and config being the configuration object used in registering the middleware.

  • middleware:before:{name} - Subscribe to this event to be notified immediately before registration of the named middleware. The event handler will receive an eventargs object containing 2 properties: app being the express application against which the middleware was registered, and config being the configuration object used in registering the middleware.

  • middleware:after - Subscribe to this event to be notified immediately after every middleware registration. The event handler will receive an eventargs object containing 2 properties: app being the express application against which the middleware was registered, and config being the configuration object used in registering the middleware.

  • middleware:after:{name} - Subscribe to this event to be notified immediately after registration of the named middleware. The event handler will receive an eventargs object containing 2 properties: app being the express application against which the middleware was registered, and config being the configuration object used in registering the middleware.

var express = require('express'),
    meddle = require('meddleware'),
    config = require('shush')('./config/middleware');

app = express();

app.on('middleware:before', function (eventargs) {
    console.log(eventargs.config.name); // depends on which middleware is about to be registered
});

app.on('middleware:before:session', function (eventargs) {
    console.log(eventargs.config.name); // 'session'
});

app.on('middleware:after:session', function (eventargs) {
    console.log(eventargs.config.name); // session
});

app.on('middleware:after', function (eventargs) {
    console.log(eventargs.config.name); // depends on which middleware is about to be registered
});

app.use(meddle(config));

Middleware Flow Control

To manage groups of middleware, there is support for parallel, race, and fallback, which allow you to register middleware intended to be run using each type of flow control. Additionally, these registration types are composable.

Parallel

Middleware designated as parallel will all be executed simultaneously, continuing processing of the remaining middleware stack only when all have completed.

{
     "cookieParser": {
         "enabled": false,
         "priority": 10,
         "module": {
            "name": "cookie-parser",
            "arguments": [ "keyboard cat" ]
        }
     },

    "setup": {
        "enabled": true,
        "priority": 20,
        "parallel": {
            "service1": {
                "enabled": true,
                "module": "./lib/middleware/service1"
            },
            "service2": {
                "enabled": true,
                "module": "./lib/middleware/service2"
            },
            "service3": {
                "enabled": true,
                "module": "./lib/middleware/service3"
            }
        }
    },

    "json": {
        "enabled": true,
        "priority": 30,
        "module": {
            "name": "body-parser",
            "method": "json"
        }
    }
Race

Middleware designated as race will all be executed simultaneously, continuing processing of the remaining middleware stack when the first has completed.

{
     "cookieParser": {
         "enabled": false,
         "priority": 10,
         "module": {
            "name": "cookie-parser",
            "arguments": [ "keyboard cat" ]
         }
     },

    "setup": {
        "enabled": true,
        "priority": 20,
        "race": {
            "service1a": {
                "enabled": true,
                "module": "./lib/middleware/service1a"
            },
            "service1b": {
                "enabled": true,
                "module": "./lib/middleware/service1b"
            }
        }
    },

    "json": {
        "enabled": true,
        "priority": 30,
        "module": {
            "name": "body-parser",
            "method": "json"
        }
    }
Fallback

Middleware designated as fallback will execute each middleware in series until one completes successfully.

{
     "cookieParser": {
         "enabled": false,
         "priority": 10,
         "module": {
             "name": "cookie-parser",
             "arguments": [ "keyboard cat" ]
         }
     },

    "setup": {
        "enabled": true,
        "priority": 20,
        "fallback": {
            "primaryService": {
                "enabled": true,
                "priority": 10,
                "module": "./lib/middleware/primaryService"
            },
            "secondaryService": {
                "enabled": true,
                "priority": 20,
                "module": "./lib/middleware/secondaryService"
            }
        }
    },

    "json": {
        "enabled": true,
        "priority": 30,
        "module": {
            "name": "body-parser",
            "method": "json"
        }
    }

Tests

$ npm test

Coverage

$ npm run cover && open coverage/lcov-report/index.html
```