title: Logging all requests in Node.js/Express
body: <p>In my small node.js application, using express, I wanted to log all the incoming requests, so I ended up with this:</p>
  
  <pre><code>var bodyParser = require('body-parser');
  
  module.exports = function(app) {
     app.set("port", 50001);
     app.set("json spaces", 2);
     app.use(bodyParser.json());
     app.use(function (error, req, res, next) {
        app.logger.info("received from "+req.get("X-Forwarded-For")+" : "+req.method+" "+req.originalUrl+" (Authorization: "+req.get("Authorization")+")");
        //does not work if json is malformed
        //app.logger.info("content :"+JSON.stringify(req.body));
        if (error /*instanceof SyntaxError*/) {
           res.status(400);
           app.logger.error(error);
           res.json({ error:{msg: error.message}});
        } else {
           next();
        }
     });
     app.use(app.auth.initialize());
  };
  </code></pre>
  
  <p>Unfortunately, I only get the logs via the <code>app.logger.info</code> line when there's an error (in my case a malformed JSON string in the body). What am I missing here?</p>