13

我已经成功让 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也没有运气。

任何让 Phantom + Node 在 Heroku 上玩得很好的人请告诉我。在 Stackoverflow 上有几个关于此的参考,但没有人说“我让它工作,这就是方法”。

4

2 回答 2

2

我从未使用过 phantomjs 节点模块,但我确实有一个应用程序在 Heroku 上同时运行节点和 phantomjs。

您需要使用自定义构建包才能使其正常工作。我的.buildpacks文件看起来像

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 */ });
于 2013-08-22T03:53:47.970 回答
0

Heroku 不支持 WebSocket。使用 Socket.io 它有一个解决方法。不确定dnode, 哪个phantomjs-node使用。

我在 Heroku 上也遇到了 WebSockets 的问题,我切换到Nodejitsu,它为我解决了这个问题。

于 2013-01-27T21:58:31.680 回答