title: Why is res.send being called twice?
body: <p>I'm working on creating a middleware for an express router that will execute some code for every request and response.  Intercepting a request is easy and there are plenty of examples, but I have not found anything for an elegant approach to intercepting a response.  After some research, the best I have come up with is to replace the send function of the response object, shown in the following snippet:</p>
  
  <pre><code>const express = require('express');
  const app = express();
  var router = express.Router();
  var port = process.env.PORT || 3000;
  
  router.get('/test', function(req, res) {
      res.send({
          message: "testing"
      });
  });
  
  app.use(function(req, res, next){
      console.log("INTERCEPT-REQUEST");
      const orig_send = res.send;
      res.send = function(arg) {
          console.log("INTERCEPT-RESPONSE");
          orig_send.call(res, arg);
      };
      next();
  });
  
  app.use("/api", router);
  app.listen(process.env.PORT || 3000) &amp;&amp; console.log("Running");
  </code></pre>
  
  <p>There's a problem with this approach: for some reason "INTERCEPT-RESPONSE" is printed in the console twice, meaning res.send is being called twice...</p>
  
  <p>I can set a flag on res.locals the first time it's called to avoid processing the response twice, but I am wondering why res.send is being called twice?</p>