Running multiple node express applications on single port - node.js

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.

Related

How to run express server on Shared Hosting?

My shared hosting doesn't allow me customization. But, I anyhow installed nodejs. I also wrote an express server code inside /var/www/test-server directory.
const express = require('express')
const app = express()
const port = 8080
app.get('/', (req, res) => {
res.send('Hello World!')
})
app.listen(port, () => {
console.log(`Example app listening on port ${port}`)
})
• But, I cannot access express server
• I can't use port 80.
• When I use some other ports like 8080, then I can see console output on Terminal as below:
example.work#example:/var/www/test-server$ node index.js
Example app listening on port 8080
Now, my question is —
"Is it possible to access express server with or without port through browser?"
Like: http://example.com/test-server
You must set up NGINX for the express server to be available on a Domain.
Alternatively, you can access the server at http://ipofthemachine:port
Make sure the port you are using is exposed if you have any firewall in place.

Openshift - port to use on deployment

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.

run multiple apps on different ports in nodejs express framework

I am running an application using node.js express framework on port 3000 (http://localhost:3000) in my windows machine. Also i want to run another(second) application on different port say like 3005 or 8080 etc. In my first application i didn't specified any port number and i believe expressjs by default runs on port 3000. But in second app i mentioned port number app.listen(8080) in my app.js. When i tried to start second application am getting port 3000 is already in use error. So i stopped first appliction and then only i can run second application and also it is running on both 3000 and 8080 ports. I didn't understand why it is still running on 3000 still i specified port 8080 number in app.js.
Could any one help to run both application on different ports in same instance
Thanks
If you're using express generator also check the www file in the bin folder, which probably contains a part similier to this:
var port = normalizePort(process.env.PORT || '3000');
app.set('port', port);
Change the '3000' part to the port you desire and it should run as intended.
Lastly to change port use "PORT=4000 node app/app.js" in cmd line.
Port no can be changed to any other port no. 5000,6000 etc ...
i believe expressjs by default runs on port 3000
It's true, but you can change it.
I think you run express default boilerplate app.
In this case you should update line var port = normalizePort(process.env.PORT || '3000'); in bin/www.
It worked after changing port number in www file. www file is located in bin folder that was created during enviornment setup.

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.

Resources