title: Express.js - Get the res.send value
body: <p>My question is exactly <a href="https://stackoverflow.com/questions/30267463/get-the-res-send-value-in-middleware-for-expressjs">this</a> one. The problem is that when I apply the solution to that question, nothing happens - the value of res.send isn't logged. <strong>I use express 4.16.4.</strong></p>
  
  <p>Here's my code:</p>
  
  <pre><code>/* FILE: /app.js: */
  
  ...
  const postRoute = require('./middleware/postRoute');
  const myRoute = require('./routes/myRoute');
  ...
  app.use('/', myRoute);
  app.use(postRoute);
  ...
  </code></pre>
  
  <hr>
  
  <pre><code>/* FILE: /routes/myRoute */
  
  ...
  router.post('/myRoute', [], (req, res, next) =&gt; {
      res.send({ status:'success', message:'Test success. Feels good, man.' });
      next();
  });
  ...
  </code></pre>
  
  <hr>
  
  <pre><code>/* FILE: /middleware/postRoute */
  
  const postRoute = function(req, res, next) {
      console.log('postRoute: ');
      var send = res.send;
      res.send = function(chunk, encoding){
          res.send = send;
          if (chunk) {
              console.log('postRoute chunk: ', chunk); // This statement is never reached
          }
          res.send(chunk, encoding);
      };
      next();
  };
  module.exports = { postRoute };
  </code></pre>
  
  <p>When I make a <code>POST</code> request to <code>/myRoute</code> using Postman, it logs the following: <code>postRoute:</code>, and that's it. The second <code>console.log()</code> is never reached and presumably neither is any other statement in the same <code>function()</code> as the <code>console.log()</code>.</p>
  
  <p>The question I link to at the start of my question was from 2015, when I assume they used a different version of Express.js, and that's why their solution isn't working for me? In my middleware code example, I used the code from a question that was linked in one of that question's answers, because it's solution didn't work for me. But that solution's obviously also not working, otherwise I wouldn't be here - and it's from 2012! Who even knows what version of Express they used back then!?</p>
  
  <p><strong>So to reiterate and conclude my question:</strong> How do I use middleware to log the value passed to <code>res.send()</code>?</p>
  
  <p><strong>Follow up question (but let me know if I should rather ask this in a separate question):</strong> Is there a way to call middleware after the route/response from the route instead of globally. So <code>router.post('/myRoute', [postRoute], (req, res, next) =&gt; {...});</code> instead of <code>app.use(postRoute)</code>?</p>