例如,我正在使用以下代码(名为 server.js)在节点(我正在使用 Windows)中运行最基本的 webServer:
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World\n');
}).listen(1337, '127.0.0.1');
console.log('Server running at http://127.0.0.1:1337/');
我想将其作为子进程运行,并通过以下方式成功运行:
var exec = require('child_process').exec;
var serv = exec('node server.js', function(error, stdout, stderr){
console.log('outputs & errors:' + error, stdout, stderr);
});
但是,一旦我运行第二个文件,我什么也没有收到。没有“输出和错误”,没有“服务器运行在...”。如果我在浏览器中键入 localhost:1337,我会得到“hello world”,所以我知道它正在运行。有没有办法通过管道传输“服务器正在运行......”所以我可以“确定”它正在监听?