我正在使用 superagent
从服务器接收通知流
require('superagent')
.post('www.streaming.example.com')
.type('application/json')
.send({ foo: 'bar' })
.on('data', function(chunk) {
console.log('chunk:' + chunk); // nothing shows up
})
.on('readable', function() {
console.log('new data in!'); // nothing shows up
})
.pipe(process.stdout); // data is on the screen
由于某种原因,data
和 read
事件未注册,但我可以将数据通过管道传输到屏幕。如何即时处理数据?
最佳答案
查看 pipe
的来源方法,您可以访问原始 req
对象并在其上添加监听器:
require('superagent')
.post('www.streaming.example.com')
.type('application/json')
.send({ foo: 'bar' })
.end().req.on('response',function(res){
res.on('data',function(chunk){
console.log(chunk)
})
res.pipe(process.stdout)
})
但这不会处理重定向(如果有)。
关于node.js - 流数据事件未注册,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31871473/