title: Invoking an asynchronous method inside a middleware in node-http-proxy
body: <p>I'm trying to create a proxy with node-http-proxy in Node.js that checks whether a request is authorized in a mongodb.</p>
  
  <p>Basically, I created a middleware module for the node-http-proxy that I use like this:</p>
  
  <pre><code>httpProxy.createServer(
  require('./example-middleware')(),
  9005, 'localhost'
  ).listen(8005)
  </code></pre>
  
  <p>What the middleware module does is using mongojs to connect to mongodb and run a query to see if the user is authorized to access the resource:</p>
  
  <pre><code>module.exports = function(){
  // Do something when first loaded!
  console.log("Middleware loaded!");
  
  return function (req, res, next) {
  var record = extractCredentials(req);
  var query = -- Db query --
  
  //Debug:
  log("Query result", query);
  
  db.authList.find(query).sort({
      "url.len": -1
  }, function(err, docs){
      console.log(docs);
  
      // Return the creator for the longest matching path:
      if(docs.length &gt; 0) {
          console.log("User confirmed!");
          next();
      } else {
          console.log("User not confirmed!");
          res.writeHead(403, {
              'Content-Type': 'text/plain'
          });
          res.write('You are not allowed to access this resource...');
          res.end();
      }
  
  });
  
  }
  }
  </code></pre>
  
  <p>Now the problem is that as soon as I add the asynchronous call to mongodb using mongojs the proxy hangs and never send the response back.</p>
  
  <p>To clarify: on a "User not confirmed" everything works fine and the 403 is returned. On a "user confirmed" however I see the log but the browser then hangs forever and the request isn't proxied.</p>
  
  <p>Now, if I remove the "user confirmed" and next() part outside of a callback it does work:</p>
  
  <pre><code>module.exports = function(){
  // Do something when first loaded!
  console.log("Middleware loaded!");
  
  return function (req, res, next) {
      var record = extractCredentials(req);
      var query = --- query ---
  
  
      console.log("User confirmed!");
      next();
  }
  </code></pre>
  
  <p>but I can't do that since the mongojs query is meant (rightfully I guess) to be executed asynchronously, the callback being triggered only when the db replied...</p>
  
  <p>I also tried the version without using a middleware:</p>
  
  <pre><code>http.createServer(function (req, res) {
    // run the async query here!
    proxy.proxyRequest(req, res, {
    host: 'localhost',
    port: 9000
  });
  }).listen(8001);
  </code></pre>
  
  <p>But that did not help either...</p>
  
  <p>Any clue? Note that I'm new to node.js so I suspect a misunderstanding on my side...</p>