在云代工厂node.js上运行子进程

时间:2013-02-24 21:39:01

标签: javascript node.js cloudfoundry phantomjs child-process

是否有人试图在 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);

cloudfoundry.com上的vmc push ,它在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"
  }
}

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

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

真的很感激!!

2 个答案:

答案 0 :(得分:0)

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

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

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

如果我全局安装phantomjs,当我点击localhost:3000

时,我看到有关节点控制台上phantomjs的错误参数的错误

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

答案 1 :(得分:0)

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

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

var phantomjs = require('phantomjs')
var binPath = phantomjs.path
ls = childProcess.exec(binPath + ' -h' //.etc etc