将值从PhantomJS传递给node.js

前端之家收集整理的这篇文章主要介绍了将值从PhantomJS传递给node.js前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个phantomJS脚本,通过一个node.js脚本中的一个 exec()调用执行.
现在我需要从PhantomJS脚本返回一个字符串,以便可以在节点中使用.
有办法实现吗?

节点应用:

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

dumper.js(Phantom)

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

解决方法

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

像这样的例子:

Node.js的:

child = exec('./phantomjs dumper.js',stderr);      // Always empty
        var result = JSON.parse(stdout);
    }
);

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

Here is some boilerplate如何使用PhantomJS刮.

原文链接:https://www.f2er.com/js/151875.html

猜你在找的JavaScript相关文章