说“Hello World!'在Openshift中使用Node.js

时间:2015-05-16 08:37:43

标签: node.js openshift

我在Openshift中创建了一个应用程序,并在我的计算机上创建了一个本地git仓库。我想在这里更改默认欢迎页面:http://nodejs-j4nos.rhcloud.com:3000并告诉Hello world this教程说。

所以我从本地repo中删除了index.html,并修改了server.js,粘贴在下面的代码中。提交,推动。我得到了很长时间的批准,他们接受了我的承诺。

如果我很好理解我不必停止节点并重新启动它,但Openshift会为我做这件事。但正如您所看到的,当浏览器中的打开链接(http://nodejs-j4nos.rhcloud.com:3000)为什么没有Hello World能够看到时?

var express = require('express');
var app = express();

app.get('/', function (req, res) {
  res.send('Hello World!');
});

var server = app.listen(3000, function () {

  var host = server.address().address;
  var port = server.address().port;

  console.log('Example app listening at http://%s:%s', host, port);

});
remote: Git Post-Receive Result: success        
remote: Activation status: success        
remote: Deployment completed with status: success        
To ssh://5556b4c4fcf9336abf0000de@nodejs-j4nos.rhcloud.com/~/git/nodejs.git/

这是树结构,列出了express

enter image description here

基于this SO回答我尝试修改脚本,但没有帮助:

var express = require('express');
var app = express();

app.set('port', process.env.OPENSHIFT_NODEJS_PORT || 8080);
app.set('ip', process.env.OPENSHIFT_NODEJS_IP || '127.0.0.1');

http.createServer(app).listen(app.get('port'), app.get('ip'), function(){
  console.log('Express server listening on port ' + app.get('port'));
});

app.get('/', function (req, res) {
  res.send('Hello World!');
});

3 个答案:

答案 0 :(得分:5)

链接现在正在运行:http://nodejs-j4nos.rhcloud.com显示“Hello world!”的正确脚本是

var http = require('http');
var express = require('express');
var app = express();

app.set('port', process.env.OPENSHIFT_NODEJS_PORT || 8080);
app.set('ip', process.env.OPENSHIFT_NODEJS_IP || '127.0.0.1');

http.createServer(app).listen(app.get('port'), app.get('ip'), function(){
  console.log('Express server listening on port ' + app.get('port'));
});

app.get('/', function (req, res) {
  res.send('Hello World!');
});

以下是证据:

enter image description here

谢谢你:Deployed Node app to OpenShift successfully, OpenShift still shows default page 这个问题:Node.js Deployment in openshift

您现在应该可以在/app-root/repo $ node server.js命令中写入,如果脚本有问题,它会在那里指示

答案 1 :(得分:0)

找到关于在节点上使用OpenShift的说明: openshift系统有一些集成的控制工具,以支持其“齿轮”系统,有助于控制openhift 应用和环境。

gear            control your application (start, stop, restart, etc)
                or deps with --cart      (gear start --cart mysql-5.1)
tail_all        tail all log files. This command displays the last entries
                in your log files as they are written.  You can hit 
                <ctrl>-c to exit this command.
export          list available environment variables
rm              remove files / directories
ls              list files / directories
ps              list running applications
kill            kill running applications
mysql           interactive MySQL shell
mongo           interactive MongoDB shell
psql            interactive PostgreSQL shell
quota           list disk usage

齿轮系统有其他命令。 OpenShift Gear Control,各种齿轮设备:

命令:

build                Run the build steps
deploy               Run the deploy steps
help                 Display global or [command] help documentation.
postreceive          Run the git postreceive steps
prereceive           Run the git prereceive steps
reload               Reload a cart
remotedeploy         Run the remotedeploy steps
restart              Restart a cart
restore              Restore an application
snapshot             Snapshot an application
start                Start the gear/cart
status               Get the status for a cart
stop                 Stop the gear/cart

这些东西会帮助你停止并重新启动装备吗?我从简单的'gear'命令开始。我不记得了,Express是通过NPM加载还是现在是节点的本机?曾经是NPM安装。那些不会被推到Openshift。

openshift nodejs服务器上的目录树结构是什么?

root
   \ app-root
        \ data
        \ repo   <- -  the working files for web content end up here.
        \ runtime
   \ git
   \ nodejs

在openshift依赖项中不会被推送。为此,您可以通过ssh登录并转到:     cd app-root/repocd $OPENSHIFT_REPO_DIR然后npm install tool_of_choice

答案 2 :(得分:0)

我已经知道了,也许你必须使用超过15000的端口:

https://help.openshift.com/hc/en-us/articles/202185874-I-can-t-bind-to-a-port