【发布时间】:2014-04-13 10:41:27
【问题描述】:
我想创建一个持续发送 json 对象的流式端点。我尝试使用分块传输编码,并且能够成功地使所有浏览器(chrome、firefox、safari)呈现从服务器发送的数据,如果它是 text/html 或 text/plain。但是当我使用 application/json 作为内容类型时,它在 chrome 上不起作用。代码如下:
var http = require('http');
http.createServer(function(request, response){
response.setHeader('Connection', 'Transfer-Encoding');
response.setHeader('Transfer-Encoding', 'chunked');
response.setHeader('Cache-Control', 'no-cache');
// response.setHeader('Content-Type', 'application/json');
response.setHeader('Content-Type', 'text/html');
setInterval(function(){
response.write('test </br>');
// response.write('{"test": "test"}');
}, 10);
}).listen(8000);
上面的代码按预期工作,但不能使它与 application/json 类型一起工作。我错过了什么吗?
【问题讨论】:
-
Google Chrome 在“查看源代码”模式下实时呈现输出。见
view-source:http://localhost:8000/。预期的行为是什么? -
这是预期的行为。那么,如果我通过 chrome 浏览器查看 twitter 流 api,同样的事情会发生吗?
标签: node.js http http-headers http-streaming