Node.js : How to do something on all HTTP requests in Express? Node.js : How to do something on all HTTP requests in Express? express express

Node.js : How to do something on all HTTP requests in Express?


Express is based on the Connect middleware.

The routing capabilities of Express are provided by the router of your app and you are free to add your own middlewares to your application.

var app = express.createServer();// Your own super cool functionvar logger = function(req, res, next) {    console.log("GOT REQUEST !");    next(); // Passing the request to the next handler in the stack.}app.configure(function(){    app.use(logger); // Here you add your logger to the stack.    app.use(app.router); // The Express routes handler.});app.get('/', function(req, res){    res.send('Hello World');});app.listen(3000);

It's that simple.

(PS : If you just want some logging you might consider using the logger provided by Connect)


You should do this:

app.all("*", function (req, resp, next) {   console.log(req); // do anything you want here   next();});


You can achieve it by introducing a middleware function.app.use(your_function) can be of help. app.use with accept a function that will get executed on every request logged to your server.Example:

app.use((req,res,next)=>{console.log('req recieved from client');next();//this will invoke next middleware function})