Openshift - port to use on deployment - node.js

I have the following start.js file:
var express = require('express');
var app = express();
app.use(express.static('static'));
var server = app.listen(8080, function () {
var host = server.address().address;
var port = server.address().port;
console.log('Example app listening at http://%s:%s', host, port);
});
In my NodeJs application on Openshift. However, when I run rhc tail-a app-name
I can see that there is an error of :
Error: listen EADDRINUSE :::8080
I've tried 80 and 443, and received those errors:
Error: listen EACCESS 0.0.0.0:443
Or 80
Which port should I use as default on my app?
Thanks!

Use Nginx,
Nginx (pronounced "engine x") is a web server. It can act as a reverse proxy server for HTTP, HTTPS, SMTP, POP3, and IMAP protocols, as well as a load balancer and an HTTP cache.
It isn't good practice to run your application with root privileges or directly run your application on port 80 and your port 8080 is in use. Try different port and use reverse proxy.
But if you want to run on port 80 or 443, run your application with root privileges.

Related

How do I set node app to run on a static port number on heroku?

my node app runs on port 8083 locally. when I push to heroku, how can I configure the service to run on the same port?
You cannot do it. Your code should listen on the port that you have in the PORT environment variable passed to you by the Heroku server that you can access as process.env.PORT and Heroku will listen on the outside on port 80 for HTTP and 443 for HTTPS.
See the docs:
https://devcenter.heroku.com/articles/runtime-principles
In particular:
https://devcenter.heroku.com/articles/dynos#web-dynos
https://devcenter.heroku.com/articles/http-routing
https://devcenter.heroku.com/articles/ssl-endpoint
Correct example:
// Get the port:
const PORT = process.env.PORT || 3000;
// Listen on the port:
app.listen(PORT, () => console.log('Listening on', PORT));
The default (3000 in this example) is for situations when you run it outside of Heroku (like for testing). When it is run on Heroku it should always listen on the port provided by Heroku. If it listens on some other port then Heroku will not proxy the traffic to your app correctly.

Running multiple node express applications on single port

I am want to make proxy manger application for express,
but I have error all time that I try to do require for two diffrent applications.
i am using on app.use to route between the two application.
thanks alot
david
var app1 = require('./../app1/server/server');
var app2 = require('./../app2/server/server');
var app = require('express')();
app.use("/", app1);
app.use("/app2", app2);
app.listen(80, console.log("server up"))
Error is :
event,js:85 listen eaddrinuse
The port 80 is in use by another process. try with another empty port like 8080 or 3000. You can also try by stopping the application that is using port 80, then it will work.

How get second port from Heroku for websocket

I'm using Heroku to deploy my nodejs game.
Everything works fine but the websockets on my game won't work when I deploy it to Heroku.
Heroku gives me 1 available port (in the var port).
Is there a way to get a second port where my socket can listen to?
If I try to set the socket to the same port as the app.listen, it tells me the port is already in use.
var port = process.env.PORT || 8080;
app.listen(port);
socket = io.listen(8000);

Node.js socket app on openshift

I'm trying to deploy a simple node.js socket app on OpenShift.
First I tried setting up the listener as:
var server = net.createServer(newSocket); //newSocket is a listener method
var port = 8888;
server.listen(port);
and this causes:
Error: listen EACCES
Then I researched a bit and learned that you need to listen using OPENSHIFT_NODEJS properties and set the listener like this:
var server = net.createServer(newSocket);
var ipaddr = process.env.OPENSHIFT_NODEJS_IP || "127.0.0.1";
var port = process.env.OPENSHIFT_NODEJS_PORT || 8888;
server.listen(port, ipaddr);
Now the app is started at: 127.6.253.1:8080 - however when I try to telnet it using my OpenShift app url and 8080 I get server timeout.
If you have experience with the similar situation let me know.
The code of the app I'm trying to make it work on OpenShift is at https://github.com/denimf/NodeChat
The internal port for the OpenShift app is 8080, but it is exposed externally on port 80 at the URL specified in your control panel. You can also see the app URL in the console by doing:
echo $OPENSHIFT_APP_DNS
Most of the node.js web hosting services don't support socket listener. I solved my problem by hosting the Node app on a dedicated virtual machine.

EC2 hosted Node.js application - can't remotely connect to port

Update: Turns out the only problem was that I was behind a firewall that blocked some ports, but not 8000.
Edit: TL;DR: can't connect to port 9000 remotely, but port 8000 is ok and I don't know why :(
I've got this node.js application that's running on port 8000 and another one (http-proxy) running on port 9000.
Running them on my machine is fine, but I have some problems when I put them up on a server (EC2 instance - I did open the ports in the web console security group[1]). The application works fine, but I can't connect to the proxy from outside. I tried to $ telnet localhost 9000 on the server and it connects, so I guess that's a good sign.
Another thing that I have noticed is that if I try to run the applications separately, I get the same results, i.e.: 8000 - OK, 9000 - NOTOK :<.
However, if I change the port the proxy uses from 9000 to 8000, it works. And if I switch the ports, i.e. application:9000 and proxy:8000, I can connect to the proxy, but not to the application. I have also tried other numbers, but that wouldn't fix it either.
I guess there's something really stupid that has nothing to do with the application itself and that I'm missing, but I can't put my finger on it, so does anyone have any idea why this setup doesn't work?
server.js
var express = require('express.io');
var app = module.exports = express();
require('./proxy');
app.http().io();
app.listen(8000);
// ...
proxy.js
var httpProxy = require('http-proxy');
var url = require('url');
httpProxy.createServer(function(req, res, proxy) {
// ...
proxy.proxyRequest(req, res, {
host: destination.host,
port: 80
});
}).listen(9000);
$ netstat -pln | grep node output
tcp 0 0 0.0.0.0:9000 0.0.0.0:* LISTEN 1487/node
tcp 0 0 0.0.0.0:8000 0.0.0.0:* LISTEN 1487/node
Security group rules
It turned out that the problem was not at all related to the application or the EC2 instance setup.
The network I was in while testing this was blocking some ports. This is why when moving the proxy to port 8000 it was working fine, but on 9000 or any other random ones that I tried it wasn't. D'oh!

Resources