how to pass multiple params in exec in node js

Viewed 2158

I'm using exec library to call R-script from node js express. Below is my code :

var exec = require("child_process").exec;

var param1 = some url;
var param2 = "hello";
var param3 = "world"
exec('Rscript pathtoscript/myScript.R"+" "+param1+" "+param2 , function(error, stdout, stderr) {
        if (error) {
            console.log(error);
            res.send(error);
        }
        else if (stderr) {
            console.log(stderr);
            res.send(stderr);
        }
        else if (stdout) {
            console.log("RAN SUCCESSFULLY");
            res.json(stdout);
        }
    });

In the above code, if I pass only param2 and param3 the r script is able to identify it. But when I pass url, only some part of url is is getting identified as URL and rest is not (may be its long). Please suggest. Thanks

1 Answers

This is not an exact answer, but I hope it helps debug the issue;

What I tried to do here is double quote param1, and separate the command from the exec to make it a little easier to read.

Best of luck.

var exec = require("child_process").exec;

var param1 = "\"http:\\something\"";
var param2 = "hello";
var param3 = "world"
var command = "Rscript pathtoscript/myScript.R "+ param1 + " " + param2 + " " + param3;

exec(command, function(error, stdout, stderr) {
        if (error) {
            console.log(error);
            res.send(error);
        }
        else if (stderr) {
            console.log(stderr);
            res.send(stderr);
        }
        else if (stdout) {
            console.log("RAN SUCCESSFULLY");
            res.json(stdout);
        }
    });
Related