从Nodejs App(Meteor)请求phantomjs动作

时间:2017-01-09 12:09:27

标签: node.js meteor phantomjs child-process

我正在尝试渲染网页并将其转换为使用Phantomjs的图像。 当我直接通过终端运行phantomjs代码时,它可以工作。当我通过node.js child_process请求运行代码时,我不断收到错误,例如' select:invalid argument' - 这个错误无限循环,所以我必须重新启动服务器才能结束这个过程。

我的phantomDriver.js示例:

var page = require('webpage').create(),
  system = require('system');

page.viewportSize = {
  width: 1920,
  height: 1080
};

/**
 * Check for required parameters
 */
if (system.args.length < 2) {
  console.log('Usage: report.js <some URL>');
  phantom.exit();
}

page.open('https://github.com/', function(status) {
    console.log('Page Loaded');
    page.render('github.png');
    phantom.exit();
});

my meteor对phantomDriver.js的请求:

var phantomjs = Npm.require('phantomjs');
var spawn = Npm.require('child_process').spawn;

Meteor.methods({
    runPhantom: function(options){
      command = spawn(phantomjs.path, ['assets/app/phantomDriver.js', '']);
      command.stdout.on('data',  function (data) {
        console.log('stdout: ' + data);
      });
      command.stderr.on('data', function (data) {
        console.log('stderr: ' + data);
      });
      command.on('exit', function (code) {
        console.log('child process exited with code ' + code);
      });
    }
});

如果我更换&#39; https://github.com/&#39;与&#39;&#39;在page.Open()方法中我得到: 使用代码0退出子进程。看起来phantomjs正在运行,但是打开/加载网页有些不对劲?

***添加评论: 看来当我运行page.open(url,function(status){})时,page.open函数回调中的任何代码都不会被执行,之前发生的事情就是无限循环&lt; - 不是100%肯定但是什么它看起来像。

谢谢

0 个答案:

没有答案