将值从PhantomJS传递到node.js

sil*_*min 10 node.js phantomjs

我有一个phantomJS脚本,通过exec()node.js脚本中的调用执行.现在我需要从PhantomJS脚本返回一个字符串,以便可以在节点中使用它.
有没有办法实现这一目标?

节点应用:

child = exec('./phantomjs dumper.js',
    function (error, stdout, stderr) {
        console.log(stdout, stderr);      // Always empty
    });
Run Code Online (Sandbox Code Playgroud)

dumper.js(幻影)

var system = require('system');
var page = require('webpage').create();
page.open( system.args[1], function (status) {
    if (status !== 'success') {
        console.log('Unable to access the network!');
    } else {

        return "String"; // Doesn't work
    }
    phantom.exit('String2'); //Doesn't work either
});
Run Code Online (Sandbox Code Playgroud)

3on*_*3on 10

是的只是从PhantomJS输出一个JSON字符串,JSON.stringify(result)并在node.js中解析它JSON.parse(stdout).

像这样例如:

Node.js的:

child = exec('./phantomjs dumper.js',
    function (error, stdout, stderr) {
        console.log(stdout, stderr);      // Always empty
        var result = JSON.parse(stdout);
    }
);
Run Code Online (Sandbox Code Playgroud)

PhantomJS:

var system = require('system');
var page = require('webpage').create();
page.open( system.args[1], function (status) {
    if (status !== 'success') {
        console.log('Unable to access the network!');
    } else {

        console.log(JSON.stringify({string:"This is a string", more: []}));
    }
    phantom.exit();
});
Run Code Online (Sandbox Code Playgroud)

这是一些如何使用PhantomJS刮擦的样板.