2017-01-23 7 views
1

노드 js의 하위 프로세스에 인수 중 일부를 전달하려고했습니다. 실제로 자식 프로세스 내부에서 팬텀 스크립트를 실행하고 있습니다. 저는 자식 스크립트를 실행하기 위해 execFile 메서드를 사용하고 있습니다. 이것은 내 index.js가 어떻게 보이는지입니다.execfile 메소드에 종속성이있는 nodejs의 하위 프로세스에 인수 전달

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); 
     } 
    }); 

그리고 제가 통과 한 인수를 인쇄하려고합니다. 그러나 아무 것도 인쇄하지 않습니다. 이것은 자식 프로세스에서 인쇄하려고하는 방법입니다.

console.log (process.argv [1]);

+0

'argv'의 모든 요소를보고 당신이 찾고있는 것이 다른 인덱스가 아닌지 확인 했습니까? 'node script.js someArg'로 명령 행에서 스크립트를 시작했다면, 첫번째 실제 명령 행 인자'someArg'는'argv [2]'에 있습니다. – jfriend00

+0

@ jfriend00 내 경우에는 아무 것도 인쇄하지 않습니다. 나는 또한 argv [2] 인쇄를 시도했다. 아무것도 인쇄되지 않습니다. – Vikas

답변

1

당신은 ThumbnailCreator.js이처럼 구문 분석 할 수

VAR 시스템 = ('시스템')을 필요로; var args = system.args;

인수 [0] 파일 이름

입니다

인수 [1] 첫 번째 인수로 전달 가치를 제공 할 것입니다.