samedi 7 mars 2015

expressjs - piping to response stream doesn't work

I have this basic express app:



var express = require('express');
var app = express();
var PORT = 3000;
var through = require('through');


function write(buf) {
console.log('writing...');
this.queue('okkkk');
}

function end() {
this.queue(null);
}

var str = through(write, end);


/* routes */
app.get('/', function(req, res){
res.send("Hello!");
})


app.post('/stream', function(req, res){
var s = req.pipe(str).pipe(res);
s.on('finish', function() {
console.log('all writes are now complete.'); // printed the first time
});
});


/* listen */
app.listen(PORT, function () {
console.log('listening on port ' + PORT + '...');
});


When I post some data to /stream endpoint for the first time after starting the server I get okkk as the response which is what I expect. However, after that, any requests to /stream endpoint just timeout and not return any response.


Why is it so? What's exactly happening here?


Aucun commentaire:

Enregistrer un commentaire