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

前端 未结 5 1140
死守一世寂寞
死守一世寂寞 2020-11-29 05:12

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=         


        
相关标签:
5条回答
  • 2020-11-29 05:42

    Simple. .stream({transform: JSON.stringify});

    0 讨论(0)
  • 2020-11-29 05:51

    Your mongo stream is dumping objects into the res stream which can only handle strings or buffers (hence the error).

    Luckily, streams are easy to pipe together so its not too hard to make a transform stream to stringify your data.

    in node v0.10.21:

    var util = require('util')
    var stream = require('stream')
    var Transform = stream.Transform
    
    util.inherits(Stringer, Transform)
    
    function Stringer() {
      Transform.call(this, { objectMode: true } )
      // 'object mode allows us to consume one object at a time
    
    }
    
    Stringer.prototype._transform = function(chunk, encoding, cb) {
      var pretty = JSON.stringify(chunk, null, 2) 
      this.push(pretty) // 'push' method sends data down the pike.
      cb() // callback tells the incoming stream we're done processing 
    }
    
    var ss = new Stringer()
    
    db.createObjectStreamSomehow()
      .pipe(ss)
      .pipe(res)
    

    hope that helps

    0 讨论(0)
  • 2020-11-29 05:55

    Using mongoose and express:

    function(req, res){
        var stream = database.tracks.find({}).stream();
        stream.on('data', function (doc) {
            res.write(JSON.stringify(doc));
        });
        stream.on('end', function() {
            res.end();
        });
    }
    
    0 讨论(0)
  • 2020-11-29 05:59

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

    cursor.stream().pipe(JSONStream.stringify()).pipe(res);
    
    0 讨论(0)
  • 2020-11-29 06:00

    A working combination of other answers here

    app.get('/comments', (req, res) => {
      Comment.find()
        .cursor()
        .pipe(JSONStream.stringify())
        .pipe(res.type('json'))
    })
    

    http://mongoosejs.com/docs/api.html#query_Query-cursor

    • cursor() returns a Node streams3 compatible stream and is preferred over the deprecated query.stream() interface.
    • Piping to JSONStream.stringify() to combine documents into an array instead of single objects
    • Piping to res.type('json') which sets the HTTP Content-Type header to application/json and returns itself (the response stream) again.
    0 讨论(0)
提交回复
热议问题