我想制作一个流式端点,不断发送json对象。我试过使用分块传输编码,并且能够成功地使所有浏览器(chrome,firefox,safari)呈现从服务器发送的数据,如果它是text/html或text/plain。但是,当我使用application/json作为内容类型时,它不适用于chrome。下面的代码:Chrome不是呈现分块的json响应
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);
上面的代码工作正常,但不能使之与应用/ JSON类型的工作。我错过了什么吗?
谷歌浏览器以“查看源代码”模式实时显示输出。查看'view-source:http:// localhost:8000 /'。什么是预期的行为? –
这是预期的行为。那么,如果我通过Chrome浏览器查看twitter streaming API,同样的事情会发生吗? – az7ar