express global middleware not being called
Updated answer for Express 4 users from the Express 4 docs. See example from docs below. Note that app.router
is deprecated and no longer used. I also added a dummy route to make the ordering clear.
You define error-handling middleware last, after other
app.use()
and routes calls
Example:
var bodyParser = require('body-parser');
app.use(bodyParser());
app.get('/', function(req, res) {
res.send('hello world');
})
app.use(function(err, req, res, next) {
// logic
});
You should put your middleware before you use app.router
.
...
app.use (function (req, res, next) {
console.log ("inside middleware");
next();
});
...
app.use(app.router);