Node.js Express middleware: app.param vs app.use

express, node.js

Solution

I tested with this program changing the order of `app.use` vs `app.param` with express 4.10.2. The param always runs first, which makes sense because the route handler expects to be able to do `req.params.foo` and in order for that to work the param handlers need to have run.

var express = require('express');
var app = express();

app.use("/:file", function (req, res) {
  console.log("@bug route", req.params.file);
  res.send();
});

app.param("file", function (req, res, next, val) {
  console.log("@bug param", val);
  next();
});



app.listen(3003);

Run this and test with `curl localhost:3003/foo` and you get the output:

@bug param foo
@bug route foo

Problem

In the chain of calls inside Express middleware, do the app.param methods always get called before app.use?

Original source