我想将来自the npm twitter package的响应流发送回客户端。
终点看起来像
var client = new Twitter({
consumer_key: '',
consumer_secret: '',
access_token_key: '',
access_token_secret: ''
});
app.get('/', function(req,res) {
const stream = client.stream('statuses/filter', {track: 'crypto'});
stream.on('data', function(event) {
res.status(200).json({tweet: event && event.text || 'nothing'})
});
});
服务器对客户端应用程序响应一次,然后给出错误消息,表明报头已经发送Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
,我该如何对流进行响应?
谢谢。
答案 0 :(得分:1)
所以,使用
res.write().
它并没有结束,因此您可以多次添加调用,而在res.json和res.send之后,您将无法写入更多内容。