nodejs child_process在expressjs中不起作用

时间:2013-11-21 05:31:10

标签: node.js express child-process

我写了一个用于抓取网页内容的代码(phantomjs)

    var page = require('webpage').create(),
    system = require('system'),
    t, address;

    if (system.args.length === 1) {
         phantom.exit(1);
    } 
    else {
        t = Date.now();
        address = system.args[1];
        page.open(address, function (status) {
        if (status !== 'success') {
             /*console.log('FAIL to load the address');*/
        } else {
            t = Date.now() - t;
            console.log(/*'Page title is ' +*/ page.evaluate(function () {
            var head = document.getElementsByTagName("head")[0]
            var basee = document.createElement("base")
            basee.href = document.location.origin

            head.insertBefore(basee, head.firstChild);
            return document.head.innerHTML + document.body.innerHTML;
        }));
        /*console.log('Loading time ' + t + ' msec');*/
    }
    phantom.exit();
  });
}
在Nodejs中我使用了下面的代码,可以返回页面内容,退出是0

var _util=require("./util");

 _util.GetPageVisual("http://www.google.com").on("success",function(d){
    console.log(d);
})
.on("error", function (err) {
    console.log(err);

})
.on("exit",function(d){
    console.log('exit:' + d);
})

现在我在express中使用了这段代码,它不起作用,要执行的错误事件,退出代码是-1

 exports.getPage = function (req, res) {
 params.page=req.params.url

_util.GetPageVisual(params.page)
    .on("success", function (d) {
        res.send(d);

    })
    .on("error", function (err) {
        console.log("error":err);

    })
    .on("exit",function(d){
        console.log('子进程已退出,代码:' + d);
    })
/*getPage(params.page).on("success", function (d) {
 res.send(d);
 })
 */

};

0 个答案:

没有答案
相关问题