1

有没有人尝试在Cloud Foundry 的 node.js上运行子进程?

我有以下代码在本地运行良好:

var port = (process.env.VMC_APP_PORT || 3000),
    host = (process.env.VCAP_APP_HOST || 'localhost'),
    http = require('http');


 var childProcess = require('child_process'),
         phantom = require('phantomjs'),
     ls;

 http.createServer(function(req, res) {

    ls = childProcess.exec('phantomjs -h', function (error, stdout, stderr) {
    if (error) {
     console.log(error.stack);
     console.log('Error code: '+error.code);
     console.log('Signal received: '+error.signal);
    }
    console.log('Child Process STDOUT: '+stdout);
    console.log('Child Process STDERR: '+stderr);
    });

    ls.on('exit', function (code) {
        console.log('Child process exited with exit code '+code);
      res.writeHead(200, {'Content-Type': 'text/plain'});
      res.write('Working...');
      res.end();
    });

 }).listen(port, host);

但是vmc pushcloudfoundry.com上,它给了我错误vmc logs

Reading logs/stdout.log... OK
Child process exited with exit code 127
Error: Command failed: /bin/sh: phantomjs: not found

    at ChildProcess.exithandler (child_process.js:536:15)
    at ChildProcess.EventEmitter.emit (events.js:91:17)
    at maybeClose (child_process.js:634:16)
    at Socket.ChildProcess.spawn.stdin (child_process.js:805:11)
    at Socket.EventEmitter.emit (events.js:88:17)
    at Socket._destroy.destroyed (net.js:358:10)
    at process.startup.processNextTick.process._tickCallback (node.js:244:9)
Error code: 127
Signal received: null
Child Process STDOUT:
Child Process STDERR: /bin/sh: phantomjs: not found

Child process exited with exit code 127
Error: Command failed: /bin/sh: phantomjs: not found

    at ChildProcess.exithandler (child_process.js:536:15)
    at ChildProcess.EventEmitter.emit (events.js:91:17)
    at maybeClose (child_process.js:634:16)
    at Socket.ChildProcess.spawn.stdin (child_process.js:805:11)
    at Socket.EventEmitter.emit (events.js:88:17)
    at Socket._destroy.destroyed (net.js:358:10)
    at process.startup.processNextTick.process._tickCallback (node.js:244:9)
Error code: 127
Signal received: null
Child Process STDOUT:
Child Process STDERR: /bin/sh: phantomjs: not found

然后我还在package.json下面添加了文件,仍然是同样的错误:

{
  "name" : "mytestchildprocesses",
  "version" : "0.1.0",
  "dependencies" : {
    "phantomjs" : "1.8.1-3",
    "child_process" : "0.x.x",
    "http" : "0.0.0"
  }
}

或者,我尝试了其他人将 phantomjs 放在 node.js 上:https ://github.com/sgentle/phantomjs-node 。但是这个选项甚至在我的本地机器上都不起作用(在 Windows 中不能调用 phantomjs.cmd)。

如果你们可以提供帮助,我宁愿想办法在 cloudfoundry.com 中执行子进程。

非常感谢!!

4

3 回答 3

1

看起来 Phantom.js 需要一个名为“phamtom”的二进制文件。我建议看一下 node-fibers 包 - https://github.com/laverdet/node-fibers。该库不依赖任何本机二进制文件,应该可以正常工作。

于 2013-02-26T00:23:22.223 回答
0

确保 'phantomjs' 作为依赖项列在你的 package.json 中(不要与 'phantom' 包混淆)。

从那里您可以使用以下代码段获取幻像可执行文件的二进制路径。phantomjs 包确保 phantom 包安装在系统上。这也适用于云代工。

var phantomjs = require('phantomjs')
var binPath = phantomjs.path
ls = childProcess.exec(binPath + ' -h' //.etc etc
于 2014-12-09T15:17:17.720 回答
0

所以我只是在本地尝试了您的代码并遇到了问题。

有了这个 package.json npm install 给了我一个关于 npm repo 中不存在的 child_process 的错误。删除该行可以使 npm install 完成。

然后我收到有关 phantomjs 无法在路径上执行的错误。

如果我全局安装 phantomjs,当我点击 localhost:3000 时,我会在节点控制台上看到有关 phantomjs 参数不正确的错误

我对上传到 Cloud Foundry 的问题并不感到惊讶,因为它肯定需要一个有效的 package.json 来启用相关节点模块的远程安装,如果它依赖于全局安装,它也会有问题。

于 2013-02-24T23:20:26.127 回答