Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stream from a mongodb cursor to Express response in node.js

Tags:

I am toying around with all the fancy node.js/mongodb/express platforms, and stumbled across a problem:

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);       }   }); }); 

As you probably guessed, catalog.byTag(tag, callback) does a find() query to Mongodb and returns the cursor

This leads to an error:

TypeError: first argument must be a string or Buffer 

According to mongodb driver doc, I tried to pass this converter to stream():

function(obj) {return JSON.stringify(obj);} 

but that does not help.

Can anybody tell me how to correctly stream something to a response?

Or is the only solution a boilerplate to manually pump the data using the 'data' and 'end' events?

like image 950
h0ru5 Avatar asked Nov 18 '13 21:11

h0ru5


People also ask

How does MongoDB cursor work?

In MongoDB, when the find() method is used to find the documents present in the given collection, then this method returned a pointer which will points to the documents of the collection, now this pointer is known as cursor.

How do I stream data in node JS?

Chaining is a mechanism to connect the output of one stream to another stream and create a chain of multiple stream operations. It is normally used with piping operations. Now we'll use piping and chaining to first compress a file and then decompress the same.


1 Answers

Use the cursor stream in combination with JSONStream to pipe it to your response object.

cursor.stream().pipe(JSONStream.stringify()).pipe(res); 
like image 140
robertklep Avatar answered Nov 25 '22 11:11

robertklep