title: Stream from a mongodb cursor to Express response in node.js
body: <p>I am toying around with all the fancy node.js/mongodb/express platforms, and stumbled across a problem:</p>
  
  <pre><code>app.get('/tag/:tag', function(req, res){
    var tag=req.params.tag;
    console.log('got tag ' + tag + '.');
    catalog.byTag(tag,function(err,cursor) {
       if(err) {
         console.dir(err);
         res.end(err);
       } else {
         res.writeHead(200, { 'Content-Type': 'application/json'});
  
         //this crashes
         cursor.stream().pipe(res);
  
       }
    });
  });
  </code></pre>
  
  <p>As you probably guessed, <code>catalog.byTag(tag, callback)</code> does a <code>find()</code> query to Mongodb and returns the cursor</p>
  
  <p>This leads to an error:</p>
  
  <pre><code>TypeError: first argument must be a string or Buffer
  </code></pre>
  
  <p>According to <a href="http://mongodb.github.io/node-mongodb-native/api-generated/cursorstream.html">mongodb driver doc</a>, 
  I tried to pass this converter to <code>stream()</code>:</p>
  
  <pre><code>function(obj) {return JSON.stringify(obj);}
  </code></pre>
  
  <p>but that does not help.</p>
  
  <p>Can anybody tell me how to correctly stream something to a response?</p>
  
  <p>Or is the only solution a boilerplate to manually pump the data using the 'data' and 'end' events?</p>