node-phantom createPage() never calls callback

2019-05-07 12:55发布

问题:

I used nodejs with node-phantom module for some time. It worked fine. Now I try it on another machine and same code example don't work:

var Scan=function(request,response)
{
    var parsedURL=url.parse(request.url,true);
    if(parsedURL.query.site)
    {
        console.log("scanning "+parsedURL.query.site);
        phantom.create(function(err,ph) {
            console.log(err);
            return ph.createPage(function(err,page) {
                console.log(err);
                return page.open(parsedURL.query.site, function(err,status) {
                    console.log("opened site? ", status);
                    if (status=="fail") {
                        response.writeHead(404, {'Content-Type': 'text/plain'});
                        response.end('URL not found');
                        return;
                    }
                    var filename="temp.jpg';
                    console.log(filename);
                    page.render(filename,function(err){
                        if (err) {
                            console.log(err);
                            return;
                        }

                        page.close(function(){
                            response.writeHead(404, {'Content-Type': 'text/plain'});
                            response.end('URL not found');
                        });
                    });
                   console.log("opened site? ", status);
                if (status=="fail") {
                    response.writeHead(404, {'Content-Type': 'text/plain'});
                    response.end('URL not found');
                    return;
                }
                var filename="temp.jpg';
                console.log(filename);
                page.render(filename,function(err){
                    if (err) {
                        console.log(err);
                        return;
                    }

                    page.close(function(){
                        response.writeHead(404, {'Content-Type': 'text/plain'});
                        response.end('URL not found');
                    });
                });
             });
           });
        });
    }
}

It never gets inside createPage() callback and it looks like lack of communication between nodejs and phantomjs. nodejs version: 0.10.10 phantomjs version: 1.9.1

How can I check what wrong with it?

UPD: Installed new Debian distro and now it throws folowing warning in the console sometimes.

warn - client not handshaken client should reconnect

It looks like socket.io problem.

回答1:

Well hard to diagnose exactly but I tried your code with the following fixes for the mismatched quotations and it seems to work fine on v0.10.6. These 2 lines that look like:

var filename="temp.jpg';

need fixing first.

My best guess is that you have a 32 bit versus 64 bit problem... seeing as you switch machines and it fails or works. So I simulated that by installing 32 bit node on a 64 bit system to show the troubleshooting procedure for that sort of thing:

First check the exit code:

[node@hip1 blah]$ phantomjs
[node@hip1 blah]$ $?
-bash: 127: command not found

follow all the symlinks and run the executable directly, for example on my system:

[node@hip1 blah]$ which phantomjs
~/node/bin/phantomjs
[node@hip1 blah]$ ls -l ~/node/bin/phantomjs
lrwxrwxrwx. 1 node node 43 Jun 16 20:06 /node/node/bin/phantomjs -> ../lib/node_modules/phantomjs/bin/phantomjs
[node@hip1 blah]$ ls -l /node/node/lib/node_modules/phantomjs/bin/phantomjs
-rwxr-xr-x. 1 node node 540 Jun 16 20:14 /node/node/lib/node_modules/phantomjs/bin/phantomjs

execute that...

[node@hip1 blah]$ /node/node/lib/node_modules/phantomjs/bin/phantomjs
/node/libs/node-v0.10.6-linux-x86/lib/node_modules/phantomjs/lib/phantom/bin/phantomjs: error while loading shared libraries: libfreetype.so.6: cannot open shared object file: No such file or directory

Ah, notice the .6 on the end of that library, it's that this is a 64 bit system but we have installed 32 bit node to avoid memory issues and have also noticed better performance with it. So an npm install phantomjs goes and gets the 32 bit version of that. So now I'd need the devel i686 versions of those libraries, which won't be installed if I don't specify - instead I'll get the x86_64 versions. So do a few of these:

yum install freetype-devel.i686 or on debian use apt-get install. You might also need libfontconfig.so.1, which is in fontconfig-devel.i686.

And finally!

phantomjs>

After that things should probably work.