title: unable to capture context in node.js
body: <p>I am trying to understand how closures work in node.js</p>
  
  <p>I have an express middleware grabbing the content of a remote site (*json). the situation here is that I am unable to update the express request (I think its possible) with the remote content to further process it in other middleware.</p>
  
  <p>any help is welcomed :)</p>
  
  <p>regards</p>
  
  <pre><code>    const request = require('request');
      const app = require('express')();
      const base = { url: 'http://api.xxx', record: 'todo' };
  
      function getFile(opts, cb) { // parse url using request
        request({
          url: opts.url,
          headers: {
              'User-Agent': 'request',
              'Content-type': 'application/json'
          }
        }, function (error, response, body) {
          if (error || response.statusCode != 200) {
              cb(error);
          } else {
              var ret = {content: JSON.parse(body), request: opts}
              cb(null, ret);
          }
        });
      }
  
      function grabData(req, res, next) {
      console.log(new Date(), req.method, req.url);
        getFile(base, function(err, data){
  
          console.log(data.content); // WORKS OK!
  
          //? How to get data.content injected in the request
          // req.contents = data.content // not working
  
             next();
        });
  
      }
  
  // function calls ---------------------
  
  app.use(grabData);
  
  //app.use(furtherProcess)  //? how to pass args here?
  
  app.get('/',  function(req,res){
      res.send("&gt; "+req.contents);
      res.end();
  });
  
  app.listen(3000, function(){ console.log('Listen on port 3000...' ); });
  </code></pre>