Question: I am building an app in which I am trying to build my own logging system for each request.

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.

I have the following code for the moment :

```js
// 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) => {
  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) => {
  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();
}
```

Unfortunately, even if I do `res.status(201).send('hello world')` It will always catch a `200` status code...
Is there a way to catch any response outgoing to the client and fetch its status code ?

Answer: 
Create middleware and Override send function

```
app.use(function (req, res) {
    var send = res.send;
    res.send = function (body) {
        // Do something
        send.call(this, body);
    };
});

```
What I understand is you need to add some status codes in the response

You can simply set status codes like this

```
let code = {
   serverError:500,
   forbidden:401
}

res.status(code.serverError).json({
  message:'Server error'
})

```

If you want to set in the middleware 

**middleware.js**

```
const statuscode = function middleware(req,res,next){

    let code = {
       serverError:500,
       forbidden:401
    }

    req.statusCode =code.serverError
   next()

}

module.exports = {
  statuscode
}

```

In the **index.js**

```
const middleware = require('./middleware');

app.get('/',middleware.statuscode, (req, res, next) => {
  console.log("Code from middleware",req.statusCode);
  res.send('hello world !');
});

```