title: How to bypass an express middleware?
body: <p>I'm working with an express application. There are some express routes, as </p>
  
  <pre><code> server.get('*' , ... )
  </code></pre>
  
  <p>etc. which perform some common operations: authentication, validation... etc.</p>
  
  <p>they also decorates the response with meaningful information: i.e. in every request to the server it gives not only the expected json/html, but also information regarding the user, some app metadata that the front-end consumes etc. etc.</p>
  
  <p>Let's say all this extra metadata cames in a field called <code>extradata</code> in <strong>every</strong> request to the server.</p>
  
  <p>Now, there is a bug that is causing a problem: instead of returning its expected response (a json with a bunch of system logs), is sending <strong>only</strong> this <code>extradata</code> field.</p>
  
  <p>I'm pretty confident the problem is in one of the middlewares, because that code that sends the response in this case is really simple, it's just a <code>res.send()</code> of a json. So I believe this part of the app is requiring some module that sets a middleware which causes the error. There are <strong>a lot</strong> of global vars and implicit parameters in the app so is really difficult to debug it manualluy.</p>
  
  <p>I attempted to bypass such middlewares programmatically, like:</p>
  
  <pre><code>delete server._router.stack[2];
  </code></pre>
  
  <p>but is causing an <code>TypeError: Cannot read property 'route' of undefined</code> and thus preventing my app to build: sure this is not the way to go.</p>
  
  <p>so, <strong>is there a way to programmatically ignore or bypass express routes that are yet set</strong>? </p>
  
  <p>Even better, <strong>is there a way to programmatically tap into express middlewares and log every request and response</strong>?</p>
  
  <p>(afaik, there are libreries like <a href="https://github.com/expressjs/morgan" rel="nofollow noreferrer">morgan</a> that logs every request, but I don't think they apply to this case since I need to discriminate between middlewares).</p>