这是一篇面向初学者的Node教程。为什么我的代码不起作用?它和他的完全相同,我已经尝试了各种解决方法但都没有成功。如果您能帮忙,请告诉我。
在这个教程中,他将一个日志/文本文件写入到位于localhost:4000的网页上,之前的示例可以正常工作(教程1),但我无法让它做任何事情,它只是运行而已。
请问有人能够将文件打印到网页上吗?谢谢!
链接:http://nodetuts.com/tutorials/2-webtail-nodejs-child-processes-and-http-chunked-encoding.html#video
我尝试了以下方法,但它们没有任何效果:
而且。
在这个教程中,他将一个日志/文本文件写入到位于localhost:4000的网页上,之前的示例可以正常工作(教程1),但我无法让它做任何事情,它只是运行而已。
请问有人能够将文件打印到网页上吗?谢谢!
链接: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', '/var/log/system.log']);
request.connection.on('end', function(){
tail_child.kill();
});
tail_child.stdout.on('data', function(data){
console.log(data.toString());
response.write(data);
});
}).listen(4000);
我尝试了以下方法,但它们没有任何效果:
console.log(data.toString());
response.write(data);
response.end();
而且。
console.log(data.toString());
response.end(data);
MiguelSanchezGonzalez
回答后的编辑:
我在正确的位置添加了tail_child.stderr.pipe(process.stdout);
,这是我得到的响应:
CreateProcessW: The system cannot find the file specified.
这里肯定存在一个文件,我还测试了许多不同的路径,包括与脚本相同文件夹中的文本文件(例如'/test.txt'
)。所以也许是我错了,我只是想当然地认为它说的是我的文件。
编辑2: 我还检查了路径是否存在(从NodeJs中检查文件是否存在的最快方法中简单构建),它确实表明该文件存在。
此代码:
var file = path.normalize = ('var/log/system.log');
fs.exists(file, function(exists){
util.debug(exists ? "yep, its there":"nope");
});
console.log(file);
输出如下:
var/log/system.log
DEBUG: yep, its there