在没有 EADDRINUSE 错误的情况下在 OpenShift 上启动 NodeJS

Starting NodeJS on OpenShift without EADDRINUSE errors

我正在尝试让 NodeJS 在 OpenShift 上达到 运行,无论如何都会遇到同样的问题。这是一个例子:

UPDATE with more details <<<<<

第一个:

这段代码在 Cloud9 中直接运行良好

var  port          = process.env.PORT || "127.0.0.1";
var ipaddress      = process.env.IP || 8080;


console.log("Getting started here!");

var http = require('http');

var server = http.createServer(function(request, response) {
    console.log((new Date()) + ' Received request for ' + request.url);
    response.writeHead(200, {'Content-Type': 'text/plain'});
      response.write("Welcome to Node.js on OpenShift!\n\n");
      response.end("Thanks for visiting us!!!! \n");
});

server.listen( port, ipaddress, function() {
    console.log((new Date()) + ' Server is listening on port 8080');
});



console.log("Listening to " + ipaddress + ":" + port + "...");

然而,一旦我将它推送到我的 OpenShift 帐户,将端口和 ipaddress 变量更改为:

var ipaddress = process.env.OPENSHIFT_NODEJS_IP || "127.0.0.1";
var port      = process.env.OPENSHIFT_NODEJS_PORT || 8080;

...当我 SSH 结束时,我在 OpenShift VM 上得到这个,更改为 app-root/runtime/repo 和 运行 节点 server.js:

events.js:72
        throw er; // Unhandled 'error' event
              ^
Error: listen EADDRINUSE
    at errnoException (net.js:901:11)
    at Server._listen2 (net.js:1020:19)
    at listen (net.js:1061:10)
    at Server.listen (net.js:1129:5)

这表明该端口已在使用中,对吗?我将端口更改为 15550,因为我在 Whosebug 上的某处阅读过 我假设特定端口范围不能直接使用 - 只能通过端口转发。 你猜怎么着,我只是得到了一个不同的错误 (EACCES)。

以下是我的 OpenShift VM 上 运行ning 的进程(据我在帐户中看到的):

   PID TTY      STAT   TIME COMMAND
177800 ?        S      0:00 sshd: XXXX@pts/1
177801 pts/1    Ss     0:00 /bin/bash --init-file /usr/bin/rhcsh -i
210176 pts/1    R+     0:00 ps ax

第二个:

我想知道如何减少构建过程?当我将我的代码推送到 OpenShift 时,我得到的是:

remote: /bin/sh: warning: setlocale: LC_ALL: cannot change locale (C.UTF-8)
remote: /bin/bash: warning: setlocale: LC_ALL: cannot change locale (C.UTF-8)
remote: /bin/bash: warning: setlocale: LC_ALL: cannot change locale (C.UTF-8)
remote: sh: warning: setlocale: LC_ALL: cannot change locale (C.UTF-8)
remote: sh: warning: setlocale: LC_ALL: cannot change locale (C.UTF-8)
remote: Stopping NodeJS cartridge
remote: Wed Mar 18 2015 07:27:49 GMT-0400 (EDT): Stopping application 'nodejs' ...
remote: Wed Mar 18 2015 07:27:50 GMT-0400 (EDT): Stopped Node application 'nodejs'
remote: /bin/sh: warning: setlocale: LC_ALL: cannot change locale (C.UTF-8)
remote: /bin/bash: warning: setlocale: LC_ALL: cannot change locale (C.UTF-8)
remote: /bin/bash: warning: setlocale: LC_ALL: cannot change locale (C.UTF-8)
remote: Saving away previously installed Node modules
remote: Building git ref 'master', commit e8239d1
remote: Building NodeJS cartridge

整个过程大约需要 10 秒或更长时间。我不确定幕后到底发生了什么,但似乎很多。

那么我的第二个问题是,有没有办法缩短构建时间? 我试图在 .opengit/markers 目录中创建一个空文件,正如我在某处读到的那样,但它没有帮助改进任何东西。

感谢您的帮助! z4c

.. I am getting this on the OpenShift VM when I SSH over, change to app-root/runtime/repo and run node server.js

不要自己手动 运行 服务器。当您将代码推送到 OpenShift 时,它会停止服务器、进行构建、部署代码并启动服务器。

所以服务器已经 运行ning。这就是您收到错误的原因:听 EADDRINUSE 自己尝试 运行ning。它不会让你 运行 在你自己的端口上。这就是为什么您要 (EACCES) 尝试在 15550 上 运行 它。

如果您需要 start/stop/restart 服务器,请使用 OpenShift 的 rhc 命令或从管理控制台。

对于您的第二个问题:有没有办法缩短构建时间? 答案是肯定的。设置热部署将节省您几秒钟(通过创建一个空文件 .openshift/markers/hot_deploy)或更改脚本不进行构建,但这不是一个好主意。它正在做它应该做的事情。将停机时间减少到 0(除非 OpenShift 停机)的正确方法是使用 OpenShift 提供的负载平衡功能。它一次可以运行 2+台服务器。因此,当您推送代码时,它会停止 1 个服务器,而其他服务器仍在接受请求。一旦第一台服务器备份,它将重新启动第二台(和其余服务器)