Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is there a standard for middleware libraries? #3150

franciscop opened this issue Dec 13, 2016 · 2 comments

Is there a standard for middleware libraries? #3150

franciscop opened this issue Dec 13, 2016 · 2 comments


Copy link

I know how to create/use middleware and I'm aware of Writing middleware for use in Express apps, however I am curious about packaging/publishing middleware.

Many of the express middleware seems to be following this convention:

let middle = require('middleware-name');
let options = {};


I have used the same for a couple of projects of mine since it seems really useful way of including the options, but haven't seen it anywhere as a recommended way of doing it. Is this part of some standard/recommended way of creating middleware? Should I edit that guide to reflect this as the recommended way?

Copy link

Twipped commented Dec 13, 2016

The guide describes the structure of a middleware function as would be passed to express. What you're describing is a middleware factory, the constructor that produces the middleware function.

The factory makes makes the options object scoped to the middleware function, encapsulating its settings in order to make the middleware module more re-usable. For example, a middleware for validating user authentication might take a user type as an option.

function isLoggedIn(type) {
  return function (req, res, next) {
    if (!req.user || req.user.type != type) {
      return next(new Error('Unauthorized');
app.get('/admin', isLoggedIn('admin'), adminController);
app.get('/vendor', isLoggedIn('vendor'), vendorController);

That pattern could certainly be added to the guide, but I think it belongs under a new heading at the bottom of the page.

Copy link

@franciscop we are open to a PR with an example of creating middleware with support for options.

franciscop added a commit to franciscop/ that referenced this issue Jan 21, 2017
Following on expressjs/express#3150 , I open this PR to add documentation regarding the options parameter factory recommendation.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
None yet

No branches or pull requests

4 participants