Stream-Puffer an den Kunden in Express

Habe ich-request-handler zu senden Datei von MongoDB (GridFS) auf client wie unten, aber es verwenden data variable, so dass der Inhalt in den Speicher. Ich brauche, um diesen im streaming-Modus und senden Sie die Datei in chunks an den client. Ich kann nicht regognize wie pipe Puffer für die Antwort. Blick auf den zweiten code - es funktioniert nicht, aber etwas zeigen, was ich brauche.

Vielleicht ist es nützlich: die Daten in GridFS ist Base64 codiert, aber kann geändert werden, wenn streaming effizienter.

In-Memory-version

router.get('/get/:id', function(req,res){
  getById(req.params.id, function(err, fileId){
    new GridStore(db, fileId, "r").open(function(err, gridStore) {
        res.set('Content-Type', gridStore.contentType);

        var stream = gridStore.stream(true);
        var data = '';
        stream.on("data", function(chunk) {
            data += chunk;
        });
        stream.on("end", function() {                   
            res.send(new Buffer(data, 'base64'));                   
        });
    });
  });
});

- Streaming-Modus-version

router.get('/get/:id', function(req,res){
  getById(req.params.id, function(err, fileId){
    new GridStore(db, fileId, "r").open(function(err, gridStore) {
        res.set('Content-Type', gridStore.contentType);

        var stream = gridStore.stream(true);
        stream.on("data", function(chunk) {
            new Buffer(chunk, 'base64').pipe(res);
        });
        stream.on("end", function() {                   
            res.end();
        });
    });
  });
});

Update

Ich glaube, ich bin in der Nähe, diese zu lösen. Ich fand dies funktioniert, aber tut nicht decodieren von Base64:

new GridStore(db, fileId, "r").open(function(err, gridStore) {
    res.set('Content-Type', gridStore.contentType);
    gridStore.stream(true).pipe(res);
});
  • marioosh haben Sie sich an meine Antwort?
InformationsquelleAutor marioosh | 2014-04-25
Schreibe einen Kommentar