我试图将一些参数传递给节点js中的子进程。我实际上在子进程内运行一个幻影脚本。我正在使用execFile方法来运行我的子脚本。这是我的索引.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: https://aws.amazon.com/blogs/compute/running-executables-in-aws-lambda/
// 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);
}
});
我试图打印我已经通过的论点。但它没有打印任何东西。这就是 m 尝试在子进程中打印它的方式:
console.log(process.argv[1]);
你可以在ThumbnailCreator中像这样解析它.js
var system = require('system');var args = system.args;
args[0] 是文件名
args[1] 会给你第一个参数中传递的值。