Jat*_*tin 2 stream node.js 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端点的任何请求都会超时并且不会返回任何响应.
为什么会这样?这到底发生了什么?
当我更换时req.pipe(str).pipe(res),req.pipe(through(write, end)).pipe(res)它确实有效地确保为每个请求创建一个新的直通实例.
我遇到了同样的问题,看起来好像res没有正确完成。因此,我在自己的信息流中添加了回调,并亲自结束了查询res。那解决了我的问题:
stream.on('end', () => res.end());
stream.pipe(res);
| 归档时间: | 
 | 
| 查看次数: | 10821 次 | 
| 最近记录: |