在openshift node js app中获取请求

时间:2016-05-18 11:00:55

标签: javascript node.js openshift

我正在构建一个openshift节点js app,它必须与youtube数据API进行通信。当我使用require评论进行“git push”时,它的部署是成功的。

    /*
    var request = require('request');
    */

当我取消注释时,我收到此错误:

remote: Waiting for application port (8080) become available ...
remote: Application 'eln' failed to start (port 8080 not available)
remote: -------------------------
remote: Git Post-Receive Result: failure
remote: Activation status: failure
remote: Activation failed for the following gears:
remote: 573c3e177628e146d400004e (Error activating gear: CLIENT_ERROR: Failed to execute: 'control start' for /var/lib/openshift/573c3e177628e146d400004e/nodejs

我做得不好吗?我该如何解决?

谢谢。

编辑1:添加监听代码,我没有修改它(当我创建应用程序时它已经在这里了。)

    self.ipaddress = process.env.OPENSHIFT_NODEJS_IP;
    self.port      = process.env.OPENSHIFT_NODEJS_PORT || 8080;

    /**
     *  Start the server (starts up the sample application).
     */
    self.start = function()
    {
        //  Start the app on the specific interface (and port).
        self.app.listen(self.port, self.ipaddress, function()
        {
            console.log('%s: Node server started on %s:%d ...', Date(Date.now() ), self.ipaddress, self.port);
        });
    };

1 个答案:

答案 0 :(得分:1)

这是我正在使用openshift的app.js基本代码。

#!/bin/env node

ipaddress = process.env.OPENSHIFT_NODEJS_IP;

if (typeof ipaddress === "undefined") {
    //  Log errors on OpenShift but continue w/ 127.0.0.1 - this
    //  allows us to run/test the app locally.
    console.warn('No OPENSHIFT_NODEJS_IP var, using 127.0.0.1');
    ipaddress = "127.0.0.1";
};

var server = app.listen(8080, ipaddress, function() {
  console.log('Listening on port %d', server.address().port);
});

你能试试吗?

<强>更新

使用request在openshift上尝试后,我也遇到了此错误,但这是因为package.json在依赖项下没有request

我的依赖项现在看起来像这样,它运行正常:

  "dependencies": {
    "ejs": "^2.4.1",
    "express": "~3.4.4",
    "request": "latest" // this is added
  },