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

Closed
franciscop opened this Issue Dec 13, 2016 · 2 comments

Comments

Projects
None yet
4 participants
@franciscop

franciscop commented Dec 13, 2016

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 = {};

app.use(middle(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?

@Twipped

This comment has been minimized.

Show comment
Hide comment
@Twipped

Twipped 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');
    }
    next();
  }
}
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.

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');
    }
    next();
  }
}
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.

@hacksparrow

This comment has been minimized.

Show comment
Hide comment
@hacksparrow

hacksparrow Jan 3, 2017

Member

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

Member

hacksparrow commented Jan 3, 2017

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

franciscop added a commit to franciscop/expressjs.com that referenced this issue Jan 21, 2017

Adding option wrapper documentation
Following on expressjs/express#3150 , I open this PR to add documentation regarding the options parameter factory recommendation.

@franciscop franciscop closed this Feb 11, 2017

@dougwilson dougwilson added the ideas label Feb 11, 2017

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment