Custom middleware in Express

suggest change

In Express, you can define middlewares that can be used for checking requests or setting some headers in response.

app.use(function(req, res, next){ });    // signature

Example

The following code adds user to the request object and pass the control to the next matching route.

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

//each request will pass through it
app.use(function(req, res, next){
    req.user = 'testuser';
    next();    // it will pass the control to next matching route
});

app.get('/', function(req, res){
    var user = req.user;
    console.log(user); // testuser
    return res.send(user);
});

app.listen(3000);

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:


Web apps with Express:
* Custom middleware in Express

Table Of Contents
1 npm
2 Web apps with Express
41 cli
43 grunt
59 Hack
64 ES6
67 Redis
69 MongoDB
86 MongoDB
87 Lodash
91 CORS
105 N-API
108 Require