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?
The express use implementation returns
thisevery time and is thus always chainable. The chainability problem comes from your unnecessary use ofserver.use(server.router), which is automatically done for you by express when your first non-middleware route is added viaserver.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 doingserver.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:server.usecalls to set up your middlewareserver.get('/home')and the other HTTP method functionsExpress will add the router after 1 but before 2, and all will be well.
In the comments you claim if you omit the
server.routerline, you lose access toreq.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 havingreq.sessionundefined, and another one immediately after the session middleware having it exist.Run it and you see this on the console.
However, if you are asking why you can’t use the
servervariable 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:Because
foois 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.