我有一个基本的express应用:
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 + '...');
});
当我在启动服务器后首次将一些数据发布到
/stream
端点时,我会得到预期的响应okkk
。然而,在此之后,任何对/stream
端点的请求都只是超时而不返回任何响应。为什么会这样?这里到底发生了什么?
res.end()
来结束它,否则它会一直保持打开状态,直到超时。 - adeneo.write()
时才需要end()
,但我尝试了一下,现在加上它后我得到了一个空响应。另外,如果我需要.end()
,为什么第一次启动服务器后它会起作用? - Jatinend
事件从未被调用。(然而,第一次一切都正常运行)。已编辑问题细节以反映这一点。 - Jatin