Pavel Zagalsky
Pavel Zagalsky

Reputation: 1636

Sending Windows commands using NodeJS

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

Upvotes: 1

Views: 162

Answers (1)

jfriend00
jfriend00

Reputation: 707328

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.

Upvotes: 1

Related Questions