tried googling this one but never got the specific answers I needed. So basically I want to develop a NodeJS app on Window that will receive http requests (Rest API style) and translate them to command line commands with arguments to trigger stuff. Is there any tutorial or a specific NodeJS package I can use to do it? Cheers, Pavel
1 Answer
You probably want .spawn() or .exec() in the child process module. The referenced doc page contains an example for running an ls command via node.js which you can likely change into whatever command line you want.
var spawn = require('child_process').spawn,
ls = spawn('ls', ['-lh', '/usr']);
ls.stdout.on('data', function (data) {
console.log('stdout: ' + data);
});
ls.stderr.on('data', function (data) {
console.log('stderr: ' + data);
});
ls.on('close', function (code) {
console.log('child process exited with code ' + code);
});
You will need to be very careful with what you're doing to avoid any means of your server getting compromised.
7 Comments
Pavel Zagalsky
Interesting.. Any resource on what are the best practices for these scenarios? Thanks!
jfriend00
@PavelZagalsky - best practices for what exactly?
A.Z.
@PavelZagalsky You may also find interesting shelljs package. github.com/shelljs/shelljs
Pavel Zagalsky
I am looking for Windows commands though, not the UNIX ones. For example if I want to trigger certain files copying or editing etc..
jfriend00
@PavelZagalsky - The devil is all in the details of exactly what commands you're running and how you get the arguments. There's no general solution.
|