node.js – 在Heroku上运行Phantomjs节点时遇到问题

我成功地让Phantomjs在Heroku上工作,但现在我遇到了node.js的phantomjs-node接口的问题(见 https://github.com/sgentle/phantomjs-node).

当我试图初始化Phantom时,我看到10-15秒的延迟,然后:

> phantom stdout: ReferenceError: Can't find variable: socket

phantom stdout:   phantomjs://webpage.evaluate():1
  phantomjs://webpage.evaluate():1
  phantomjs://webpage.evaluate():1

您可以通过以下步骤重现此问题,或者在https://github.com/matellis/phantom-test下拉我的测试应用程序

git init phantom-test
cd phantom-test
heroku apps:create
# create node app as per Heroku instructions here https://devcenter.heroku.com/articles/nodejs
# copy bin and lib folders from http://phantomjs.googlecode.com/files/phantomjs-1.6.1-linux-x86_64-dynamic.tar.bz2 into root of your new project
# if you don't do this step you'll get an error "phantom stderr: execvp(): No such file or directory"
git add .
git commit -m "init"
git push heroku

测试你的应用程序已经出现,第三个最后一行会告诉你的URL,它应该看起来像:

http://fathomless-ravine-5563.herokuapp.com deployed to Heroku

如果成功,你应该看到Hello World!在你的浏览器

现在与您的Heroku应用程序运行的文件夹相同:

heroku run node

在节点提示下,尝试以下操作:

phantom = require('phantom');
x = phantom.create();

等待10-15秒,你应该看到错误.从这一点上没有什么可行的.

这应该输出文件foo.png:

x = phantom.create(function(ph){ph.createPage(function(page){ page.open('http://bbcnews.com',function(status){ page.render('foo.png',function(result) {ph.exit()}); }); }); });

验证Phantomjs在Heroku工作正常,尝试以下使用我的测试项目:

>heroku run bash
Running `bash` attached to terminal... up,run.1
~ $phantomjs test.js http://bbcnews.com foo.png
~ $ls *.png
foo.png

我无法在本地再现任何这些问题,但还有其他问题报告当地人可能会遇到这个问题.

问题似乎源于shim.js第1637行:

s.on('request',function(req) {
  var evil;
  evil = "function(){socket.emit('message'," + (JSON.stringify(JSON.stringify(req))) + " + '\\n');}";
  return controlPage.evaluate(evil);
});

我尝试过不同版本的节点,幻像等,没有运气.

我也试过一个自定义构建包,设置DYLD变量,参见http://github.com/tecnh/heroku-buildpack-nodejs,没有运气.

任何有幽灵节点在Heroku上玩得很好的人,请让我知道.在Stackoverflow中有几个引用,但没有人说“我得到了工作,这里是如何”.

解决方法

我从来没有使用过phantomjs节点模块,但是我有一个在Heroku上运行节点和phantomjs的应用程序.

您需要使用自定义的buildpacks才能使其正常工作.我的.buildpacks file看起来像

http://github.com/heroku/heroku-buildpack-nodejs.git
http://github.com/stomita/heroku-buildpack-phantomjs.git

然后,您应该可以在子进程中运行phantomjs脚本:

var script = app.get('root') + '/scripts/rasterize.js' //the phantomjs script to run,bin = app.get('phantom') //normally this would just be the string "phantomjs",spawn = require('child_process').spawn;

// set up args to the phantom cli
// (run the phantomjs command in your terminal to see options/format)
var args = [];
// ...

var phntm = spawn(bin,args);

phntm.stdout.on('data',function (data) { /* do something */ });
phntm.stderr.on('data',function (data) { /* do something */ });
phntm.on('exit',function (code) { /* handle exit */ });

相关文章

这篇文章主要介绍“基于nodejs的ssh2怎么实现自动化部署”的...
本文小编为大家详细介绍“nodejs怎么实现目录不存在自动创建...
这篇“如何把nodejs数据传到前端”文章的知识点大部分人都不...
本文小编为大家详细介绍“nodejs如何实现定时删除文件”,内...
这篇文章主要讲解了“nodejs安装模块卡住不动怎么解决”,文...
今天小编给大家分享一下如何检测nodejs有没有安装成功的相关...