我将 nodejs 与 node-phantom 模块一起使用了一段时间。它运作良好。 现在我在另一台机器上尝试,相同的代码示例不起作用:
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');
});
});
});
});
});
}
}
它永远不会进入 createPage() 回调,看起来 nodejs 和 phantomjs 之间缺乏通信。 Node 版本:0.10.10 phantomjs 版本:1.9.1
如何检查它有什么问题?
UPD:安装了新的 Debian 发行版,现在它有时会在控制台中抛出以下警告。
warn - client not handshaken client should reconnect
看起来像是 socket.io 的问题。
最佳答案
很难准确诊断,但我尝试了您的代码并针对不匹配的引号进行了以下修复,它似乎在 v0.10.6 上运行良好。这两行看起来像:
var filename="temp.jpg';
需要先修复。
我最好的猜测是你有一个 32 位和 64 位的问题......当你切换机器时看到它失败或工作。所以我通过在 64 位系统上安装 32 位 Node 来模拟,以显示此类事情的故障排除过程:
首先检查退出代码:
[node@hip1 blah]$ phantomjs
[node@hip1 blah]$ $?
-bash: 127: command not found
跟随所有符号链接(symbolic link)并直接运行可执行文件,例如在我的系统上:
[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
执行...
[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
啊,请注意该库末尾的 .6,这是一个 64 位系统,但我们安装了 32 位 Node 以避免内存问题,并且还注意到它具有更好的性能。所以 npm install phantomjs 去获取它的 32 位版本。所以现在我需要这些库的开发 i686 版本,如果我不指定它们将不会安装 - 相反我将获得 x86_64 版本。所以做其中的一些:
yum install freetype-devel.i686
或在 debian 上使用 apt-get install
。您可能还需要 libfontconfig.so.1,它位于 fontconfig-devel.i686 中。
最后!
幻影>
在那之后事情应该可以工作了。
关于javascript - node-phantom createPage() 从不调用回调,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17002351/