title: Get response status code in a middleware
body: <p>I am building an app in which I am trying to build my own logging system for each request.</p>
  
  <p>For each request, I'd like to log the timestamp, the method used, the route, and finally the response code that has been sent to the client.</p>
  
  <p>I have the following code for the moment :</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>// index.js
  const express = require('express');
  const bodyParser = require('body-parser');
  const cors = require('cors');
  
  const app = express();
  app.use(bodyParser.json());
  app.use(cors());
  
  app.use(require('./lib/logging'));
  
  app.get('/', (req, res, next) =&gt; {
    res.send('hello world !');
  });
  
  app.listen(3001);
  
  // ./lib/logging.js
  const moment = require('moment');
  const chalk = require('chalk');
  const log = console.log;
  
  module.exports = (req, res, next) =&gt; {
    let now = `[${chalk.green(moment().format('HH:mm:ss'))}]`;
    let method = chalk.magenta(req.method);
    let route = chalk.blue(req.url);
    let code = chalk.yellow(res.statusCode); // Always 200
    log(`${now} ${method} request received on ${route} ${code}`);
    next();
  }</code></pre>
  </div>
  </div>
  </p>
  
  <p>Unfortunately, even if I do <code>res.status(201).send('hello world')</code> It will always catch a <code>200</code> status code...
  Is there a way to catch any response outgoing to the client and fetch its status code ?</p>