无法在Heroku上运行Phantomjs +节点

我已经成功地让Phantomjs能够在Heroku上工作,但现在我遇到了node.js的phantomjs-node接口问题(请参阅https://github.com/sgentle/phantomjs-node )。

当我试图初始化幻影时,我看到一个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下拉testing应用程序

 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 

testing你的应用程序已经上来,倒数第三行会告诉你的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()}); }); }); }); 

要validationPhantomjs在Heroku上工作正常,请使用我的testing项目尝试以下操作:

 >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); }); 

我尝试过不同版本的节点,幻影,等等。

我也试过了一个自定义的buildpack来设置DYLDvariables,看到http://github.com/tecnh/heroku-buildpack-nodejs也没有运气。

任何在Heroku上都能很好地与Phantom + Node一起玩的人,请告诉我。 在Stackoverflow上有几个这样的引用,但是没有人说“我知道它的工作原理,这里是怎么做的”。

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

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

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

然后,您应该能够在subprocess中运行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 */ }); 

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

我在Heroku的WebSockets上也遇到了问题,于是我转向Nodejitsu ,它为我解决了这个问题。