Node.js Deployment in openshift

2019-04-08 09:14发布

问题:

I was trying to deploy a Node.js application to the openshift as in this link here

I understand this code

var http = require('http');

var server = http.createServer(function(req, res) {
    res.writeHead(200);
    res.end('Hello Http');
});
server.listen(3000);

and there is no issue running it locally

$ node server.js // saved as server.js

However, how does this work when I commit this application in openshift? This is very simple code. I have some downloaded code that is a chat application and client-server need to configure to listen on some port (I was using port number 3000 in my localhost).

It works on port number 3000 in localhost but how can I make it to work in Openshift?

回答1:

You need to listen on port process.env.OPENSHIFT_NODEJS_PORT. So something like this should work:

server.listen(process.env.OPENSHIFT_NODEJS_PORT || 3000);

See here for example: Error: listen EACCES on Openshift app



回答2:

Hey the issue with socket.io is that you have that npm package installed local but not in openshift (dependencies don't get pushed). For that you can login thru ssh (look for "Want to log in to your application?" in right menu in openshift control panel, follow instructions and use the ssh connection provided) then login with terminal o Putty, and go to:

cd app-root/repo 

or

cd $OPENSHIFT_REPO_DIR

and then

npm install socket.io

I've used that to install mongoose and other dependencies without trouble. Also you can use

node server.js

from command line to run the site ;)