简体   繁体   中英

Node.js child_process.exec()

I'm trying to exec a child process in node, the name of which is coming from a post request. I'm using express, hence app.post(). My code is below.

app.post('/*', function(req, res) {

  var buf = new Buffer(256);
  var cont_len = buf.write(req.url, -1);
  var controller = buf.toString('utf8', 0, cont_len);
  var controller = controller.concat(' ')

  var args = req.param('args', null);

  var command = __root+__controllers+controller+args;

  console.log(command);

  c = exec(command,

  function (error, stdout, stderr) {
    res.send(stdout);
    res.send(stderr);

    if (error !== null) {
      res.send(error);
    }
  });

});

The part that doesn't work is when I try and build the command string using a dynamic command. I can hardcode the command variable and it works but when i use the variable 'controller' it doesn't exec my process. Also, is there a way of responding with the return value of the child process or must I stick to only using stdout?

您可以使用req.body访问请求参数

var command = req.body.theparam;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM