简体   繁体   English

在OpenShift上启动NodeJS而不出现EADDRINUSE错误

[英]Starting NodeJS on OpenShift without EADDRINUSE errors

I am trying to get NodeJS to run on OpenShift, do encounter the same issue though no matter what. 我试图让NodeJS在OpenShift上运行,尽管遇到任何问题都遇到相同的问题。 Here is an example: 这是一个例子:

UPDATE with more details <<<<< 更新更多细节<<<<<

FIRST: 第一:

This piece of code works fine straight out of Cloud9 这段代码可以直接在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 + "..."); 

However as soon as I push it to my OpenShift account changing the port and ipaddress variables to: 但是,一旦我将其推送到我的OpenShift帐户,就将port和ipaddress变量更改为:

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

... I am getting this on the OpenShift VM when I SSH over, change to app-root/runtime/repo and run node server.js: ...当我通过SSH切换到app-root / runtime / repo并运行node server.js时,我在OpenShift VM上得到了这个:

 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) 

That would indicate the port is in use already, right? 这表明该端口已在使用中,对吗? I changed the port to 15550 as I have read somewhere here on stackoverflow I assume that a specific port range is not directly available - only via port forwarding. 我将端口更改为15550,正如我在stackoverflow上的某处阅读的那样,我认为特定的端口范围不是直接可用的-仅通过端口转发。 Guess what, I just get a different error (EACCES). 猜猜是什么,我只是得到一个不同的错误(EACCES)。

Here are the processes running on my OpenShift VM (as far as I can see with my account): 这是在我的OpenShift VM上运行的进程(据我的帐户所看到的):

  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 

SECOND: 第二:

I wonder how I can cut down the build process? 我想知道如何减少构建过程? When I push my code to OpenShift this is what I get: 当我将代码推送到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 

The whole process takes like 10 seconds or more. 整个过程大约需要10秒或更长时间。 I am not sure what exactly is happening behind the scenes but it seems to be a lot. 我不确定幕后到底发生了什么,但似乎很多。

So my second questions, is there a way to cut that build time down? 所以我的第二个问题是,有没有办法减少构建时间? I tried to create an empty file in the .opengit/markers directory as I have read somewhere, but it did not help to improve anything. 我尝试在.opengit / markers目录中创建一个空文件,正如我在某处阅读的那样,但它无济于事。

Thanks for your help! 谢谢你的帮助! z4c z4c

.. I am getting this on the OpenShift VM when I SSH over, change to app-root/runtime/repo and run node server.js ..当我通过SSH切换到app-root / runtime / repo并运行node server.js时,我在OpenShift VM上得到了这个

Don't manually run the server yourself. 不要自己手动运行服务器。 When you push your code to OpenShift, it stops the server, does the build, deploys the code, and starts the server. 当您将代码推送到OpenShift时,它将停止服务器,执行构建,部署代码,然后启动服务器。

So the server is already running. 因此服务器已经在运行。 That's why you're getting Error: listen EADDRINUSE try running it yourself. 这就是为什么出现错误:听EADDRINUSE尝试自己运行它的原因。 It won't let you run on your own port. 它不会让您在自己的端口上运行。 That's why you're getting (EACCES) trying to run it on 15550. 这就是为什么您要(EACCES)尝试在15550上运行它。

If you need to start/stop/restart the server, use OpenShift's rhc command or from the admin console. 如果需要启动/停止/重新启动服务器,请使用OpenShift的rhc命令或从管理控制台中使用。

For your 2nd question: is there a way to cut that build time down? 对于您的第二个问题:有没有办法减少构建时间? The answer is yes. 答案是肯定的。 Setting up hot deploy will save you few seconds (by creating an empty file .openshift/markers/hot_deploy) or change the script not to do the build but that's not a good idea. 设置热部署将节省您几秒钟的时间(通过创建一个空文件.openshift / markers / hot_deploy)或更改脚本以不进行构建,但这不是一个好主意。 It's doing what it's supposed to do. 它正在做应该做的事情。 The right way to cut down the down time to 0 (unless OpenShift is down) is to use the load balancing feature OpenShift offers. 将停机时间减少到0(除非OpenShift停机)的正确方法是使用OpenShift提供的负载平衡功能。 It can run 2+ servers at a time. 它一次可以运行2台以上的服务器。 So when you push your code, it stops 1 server while the other servers are still taking requests. 因此,当您推送代码时,它将停止一台服务器,而其他服务器仍在接受请求。 Once the 1st server is back up, it'll rebounce the 2nd (and the rest of the servers) 备份第一台服务器后,它将重新启动第二台(以及其余服务器)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM