使用nodeJS中的phantomJS并将参数传递给js文件

时间:2014-10-02 00:24:15

标签: node.js phantomjs

我试图使用nodeJS中的phantomJS作为产生进程,此时很好,当我尝试将一些参数传递给phantomJS使用的js文件时出现了问题,该怎么办呢?

这是我的代码:

spawn = require("child_process").spawn;
phantom = spawn("/usr/bin/phantomjs", [
                "phantom_js_file.js",
                "x xxx xxx xxx" ] );

其中x是phantom_js_file.js的参数

在我的phantom_js_file.js中我得到这样的参数:

var arg = require("system").args,
    tip = arg[1],
    rl = arg[2],
    er = arg[3],
    cod = arg[4],

任何想法都表示赞赏。 问候。

1 个答案:

答案 0 :(得分:0)

您需要将参数作为参数数组传递,其中“您通常使用的”命令信号数组分隔符中的空格。

如果您将其正常称为“phantomjs myfile.js arg1 arg2 arg3”,则需要使用spawn调用:

spawn("phantomjs", ["myfile.js","arg1","arg1","arg3"]);

所以这些参数中没有空格。如果有空格,它将成为新的数组条目。这甚至适用于像“somecommand -d thisdir”这样的事情。 “-d”和“thisdir”属于一起并不重要,spawn需要将它们分开:

spawn("somecommand", ["-d","thisdir"]);
相关问题