有没有办法传递并获取正在运行的节点应用程序的参数?寻找像信号但有自定义参数的东西。
process.on('runX', function (params) {
console.log('RunX called. Params:' + params);
});
$ passParams nodeAppPID runX param1 param2
我试图解决的最后一个问题是通过cron将params传递给应用程序。
答案 0 :(得分:1)
如评论中所述,有两种解决方案:
第二个解决方案的实用程序类: 的
class RemoteControl {
constructor(port) {
this.prefix = '/remote/';
this.handlers = [];
this.initServer(port);
}
initServer(port) {
const http = require('http');
const self = this;
const server = http.createServer(function (req, res) {
self.handleRequest(req.url);
res.statusCode = 200;
res.setHeader('Content-Type', 'text/plain');
res.end('');
});
server.listen(port, () => {
console.log(`RemoteControl running at http://localhost:${port}/`);
});
this.addCall('ping', function () {
console.log('Remote ping ok');
});
}
/**
*
* @param url {String}
*/
handleRequest(url) {
let handlerFound = false;
for (let index in this.handlers) {
let handler = this.handlers[index];
if (url === handler.name) {
console.log('handler called for ' + url);
handlerFound = true;
handler.handler();
break;
}
}
if (!handlerFound) console.log('handler not found for ' + url);
}
/**
*
* @param callName {String}
* @param handler {Function}
*/
addCall(callName, handler) {
this.handlers.push(new Handler(this.prefix + callName, handler));
}
}
class Handler {
/**
* @param name {String}
* @param handler {Function}
*/
constructor(name, handler) {
this.name = name;
this.handler = handler;
}
}
module.exports = RemoteControl;
客户端代码用法:
const remoteControl = new RemoteControl(6666)
remoteControl.addCall('hello', function(){
//handle hello call
});
Cron工作:
* * * * * curl http://localhost:6666/remote/hello