使用node.js在服务器端运行可执行文件就像调用child_process.spawn()
.
您可能希望将收集器生成的文件内容推送到客户端,
这可以通过使用Readable Stream读取文件fs.watch
来实现。您可以使用将数据广播给客户端。socket.io
更新这里是一个使用express.js的简单实现。
/**
* app.js
* make sure to install express (npm install express)
* and place the collector executable beside this file
* or set collectorPath to the correct path.
* run the app and open 'http://0.0.0.0:3000/start' in your browser.
*/
var child_process = require('child_process');
var path = require('path');
var express = require('express');
var app = express();
var collector = null;
app.get('/start', function (req, res) {
if (collector !== null) {
res.end('Collector is already running.');
return;
}
var collectorPath = path.join(__dirname, './collector');
collector = child_process.spawn(collectorPath, [], {
stdio: 'ignore'
});
collector.on('exit', function (signal, code) {
console.log('Collector exited with signal: %s and code: %d', signal, code);
collector = null;
});
res.end('Done.');
});
app.listen(3000);
console.log('Listening on port 3000');