Programming/node.js2018. 10. 17. 11:15

node를 이용하여 RPC 비스므리하게 구현해본 예제

근데 windows + node.js라 kill을 테스트 해보진 못함

var express = require('express');
var spawn = require('child_process').spawn;
var router = express.Router();

var proc_list = [];
var pid_list = [];

function find_pid(pid) {
var len = pid_list.length;
for (var i = 0; i < len; i++) {
if (proc_list[i]['pid'] == pid) {
return i;
}
}

return -1;
}

// create proces
router.post('/program', (req, res, next) => {
var cmd = req.query.cmd;
var args = req.query.args;
// console.log("req.query:"+JSON.stringify(req.query));
console.log("cmd:" + cmd);
console.log("args:" + args);
var argstring = args.split(" ");
var argslist = [];
for (var i = 0; i < argstring.length; i++) {
argslist.push(argstring[i]);
if (i != argstring.length - 1) {
argslist.push(" ");
}
}

child = spawn(cmd, argslist);
child.on('error', function (err) {
console.log(err);
});

var pid = child.pid;
console.log("cmd executed");
pid_list.push(pid);
proc_list.push({ 'pid': pid, 'obj': child });
res.send('' + pid);
});

// read process list
router.get('/program', (req, res, next) => {
console.log('get');
res.send(pid_list);
});

// update
// router.put('/program', (req, res, next) => {
// });

// delete
router.delete('/program', (req, res, next) => {
var pid = req.body.pid;
var val = find_pid(pid);
if (val != -1) {
pid_list.splice(val, 1);
proc_list[val].obj.kill();
proc_list.splice(val, 1);
}
res.send(pid + ' killed');
});

module.exports = router;

[링크 : https://stackoverflow.com/questions/18334181/spawn-on-node-js-windows-server-2012]

[링크 : https://stackoverflow.com/questions/26425637/javascript-split-string-with-white-space]

'Programming > node.js' 카테고리의 다른 글

node-rtsp-stream의 pid 얻기  (0) 2018.10.18
node.js post body  (0) 2018.10.17
node post header max size  (0) 2018.10.16
node url parametet  (0) 2018.10.16
node.js spawn 과 exec  (0) 2018.10.16
Posted by 구차니