我想在这里按照教程:为什么response.write似乎在Node.js中阻止我的浏览器?
http://nodetuts.com/tutorials/2-webtail-nodejs-child-processes-and-http-chunked-encoding.html#video
下面的代码工作
var http = require('http');
var spawn = require('child_process').spawn;
http.createServer(
function (request, response) {
response.writeHead(200, {
'Content-Type':'text/plain'
});
var tail_child = spawn('tail', ['-f', 'temp.txt']);
request.connection.on('end', function() {
tail_child.kill();
});
tail_child.stdout.on('data', function (data) {
console.log(data.toString());
response.end(data.toString());
});
}).listen(9000);
但是浏览器没有收到更新为temp.txt。如果我更换
response.end(data.toString());
与
response.write(data.toString());
这似乎阻止,并没有什么浏览器中呈现。
编辑:我想浏览器实时连续地显示任何追加到文本文件按照本教程
我已经做了一个编辑来澄清 – deltanovember
是的,因为如果你永远不会调用response.end(),那么永远不会发送响应。 –
在教程中没有response.end(),但新的附加信息似乎'流'到浏览器 – deltanovember