将参数传递给nodejs中的子进程,这些参数在execFile方法中具有一些依赖性
我试图将一些参数传递给节点js中的子进程。我实际上是在子进程中运行一个幻像脚本。我使用execFile方法运行我的子脚本。这是我的index.js的样子:
var childProcess = require('child_process');
var path = require('path');
var phantomjs = require('phantomjs');
var binPath = phantomjs.path
console.log('inside index method ');
// Set the path as described here: /
// Set the path to the phantomjs binary
//var phantomPath = path.join(__dirname, 'phantomjs_linux-x86_64');
// Arguments for the phantom script
var processArgs = [
path.join(__dirname, 'ThumbnailCreator.js'),
'myargs'
];
// Launch the child process
childProcess.execFile(binPath, processArgs, function(error, stdout, stderr) {
console.log('stdout: ', stdout);
console.log('stderr: ', stderr);
if (error !== null) {
console.log('exec error: ', error);
}
});
而我试图打印我已经通过的论点。但它不打印任何东西。这是尝试在子进程中打印它的方式:
的console.log(process.argv [1]);
回答如下:你可以在ThumbnailCreator.js中解析它
var system = require('system'); var args = system.args;
args [0]是文件名
args [1]会给你第一个参数传递的值。