简体   繁体   中英

Using nodeJS and phantomJS to return network requests and responses, only working in console

I'm trying to replicate the phantomJS netlog.js functionality, only in nodeJS. I'm using the phantomjs-node module as a bridge.

Normally, this would be run headlessly in the command line using phantomjs netlog.js http://www.google.com/ . It would return a lot of json containing all network requests and responses.

What I'm doing here is trying to run the code from netlog.js inside of a page created using the phantomjs-node module (ignoring the line var page = require('webpage').create() from netlog.js .

While the code doesn't break, I'm not getting the return of the json. What's wrong here? Do I need to somehow pipe the page request?

In app.js :

var phantom = require('phantom');

siteUrl = "http://www.google.com/"
phantom.create(function (ph) {
    ph.createPage(function (page) {
        var system = require('system'),
        address;
        page.open(siteUrl, function (status) {
        // console.log("opened " + siteUrl +"\n",status+"\n");

            page.evaluate(function () { 
                if (system.args.length === 1) {
                    console.log('Usage: netlog.js <some URL>');
                    phantom.exit(1);
                } else {
                console.log(system.args[1])
                    address = system.args[1];

                    page.onResourceRequested = function (req) {
                        console.log('requested: ' + JSON.stringify(req, undefined, 4));
                    };

                    page.onResourceReceived = function (res) {
                        console.log('received: ' + JSON.stringify(res, undefined, 4));
                    };

                    page.open(address, function (status) {
                        if (status !== 'success') {
                            console.log('FAIL to load the address');
                        }
                        phantom.exit();
                    });
                }

            }, function finished(result) {

                ph.exit();            
            },thirdLayerLinks);

        });
    });
}, {
dnodeOpts: {
    weak: false
}
});

You made a mistake during copy-paste. There shouldn't be a page.evaluate call and only one page.open call. You took a little too much from the basic phantomjs-node code.

PhantomJS and Node.js have different runtimes and vastly different modules. There is no phantom reference. Additionally there is no system in node. You probably mean process .

Then the docs say the following:

Callbacks can't be set directly, instead use page.set('callbackName', callback)

Fixed code:

var phantom = require('phantom');

var address = "http://google.com/";
phantom.create(function (ph) {
    ph.createPage(function (page) {
        page.set("onResourceRequested", function (req) {
            console.log('requested: ' + JSON.stringify(req, undefined, 4));
        });

        page.set("onResourceReceived", function (res) {
            console.log('received: ' + JSON.stringify(res, undefined, 4));
        });

        page.open(address, function (status) {
            if (status !== 'success') {
                console.log('FAIL to load the address');
            }
            ph.exit();
        });
    });
}, {
    dnodeOpts: {
        weak: false
    }
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM