Randomblue
Randomblue

Reputation: 116263

Better middleware chaining in Express

This is my Express configuration code:

var server = express()
    .use(express.cookieParser())
    .use(express.session({secret: buffer.toString('hex')}))
    .use(express.bodyParser())
    .use(express.static('./../'));

server.use(server.router);

It's quite annoying that the last .use is not chained like the rest.

How can I do full chaining of my Express middleware definitions?

Upvotes: 1

Views: 1412

Answers (1)

Peter Lyons
Peter Lyons

Reputation: 145994

The express use implementation returns this every time and is thus always chainable. The chainability problem comes from your unnecessary use of server.use(server.router), which is automatically done for you by express when your first non-middleware route is added via server.get, server.post, etc. This can can be seen in the express source code here, although this is a common point of confusion and problems (most of which come from doing server.user(server.router) at all as opposed to just letting the router go where it goes by default, which is almost always the correct place for it. So here's the forumla:

  1. Properly ordered server.use calls to set up your middleware
  2. Your specific routes with server.get('/home') and the other HTTP method functions
  3. Any error handling, 404 middleware, etc

Express will add the router after 1 but before 2, and all will be well.

In the comments you claim if you omit the server.router line, you lose access to req.session. Sorry, but this is simply not the case. Here's an entire working express app that shows a middleware immediately before the session middleware having req.session undefined, and another one immediately after the session middleware having it exist.

    var express = require('express');
    var server = express();
    server.use(function (q,r,n) {console.log('Before cookie:', q.session);n();});
    server.use(express.cookieParser());
    server.use(function (q,r,n) {console.log('after cookie:', q.session);n();});
    server.use(express.session({secret: 'SEKRET'}));
    server.use(function (q,r,n) {console.log('after session:', q.session);n();});
    server.get('/', function (q,r,n) {r.send("you got slashed");});
    server.listen(3000);

Run it and you see this on the console.

Before cookie: undefined
after cookie: undefined
after session: { cookie: 
   { path: '/',
     _expires: null,
     originalMaxAge: null,
     httpOnly: true } }

However, if you are asking why you can't use the server variable as a function parameter in the same statement in which it is defined, that's just how JavaScript works and has zero to do with express. You can't do this:

var foo = console.log(foo.bar);

Because foo is undefined until that entire statement is complete.

FWIW my opinion is that chainable code like this is dubious. It is unfriendly to debuggers and breakpoints, not supported as a first-class language feature, and isn't really saving you any characters compared to each line being unindented and app.use(app.router), but so be it.

Upvotes: 6

Related Questions