title: Node.js / Express - modifying response template context through request/response objects
body: <p>I am using Express to serve web pages in node.js application.</p>
  
  <p>Let's say I want to have a variable <code>foo</code> available in all views rendered by <code>render</code> method of response object. I know that I can define dynamic helpers for this task. However, I found them unsuitable when you need to set helper variable asynchronously like this (Mongoose example):</p>
  
  <pre><code>Thing.count(filter, function(error, thingCount) {
      foo = thingCount;
  }
  </code></pre>
  
  <p>I've tried using connect middleware approach, which suits me perfectly, however the question here is how to affect the response context. By looking into <code>render</code> method definition in <code>express/lib/view.js</code> I've found that it can be manipulated by writing into app._locals object:</p>
  
  <pre><code>function putFooIntoContext (req, res, next) {
    Thing.count(filter, function(error, thingCount) {
      res.app._locals.foo = thingCount;
      next();
    }
  }
  </code></pre>
  
  <p>It works as intended, however, I am a bit afraid that such straightforward approach is not the best solution. Can someone give me any ideas how to affect response context by interacting only with request/response objects in proper way designed by Express developers?</p>