Node.js + Express only work on port 3000 on test server - node.js

On Local Test it work. on different port (3001, 8080)
But on Test Server (Azure)
I run 2 instance of Node App on same machine
$ node api1/index.js (on port 3000)
$ node api2/index.js (on port 3001)
and
$ node api1/index.js (on port 3001)
$ node api2/index.js (on port 3000)
But it only works on port 3000.
How do I set different port in Express?
Now, I've changed at app.listen(3001) on index.js and it doesn't work.

Often cloud platforms set an environment variable that contains the port they want you to stick your app on. I don't have any experience with Azure... See the answer here: How to run a node.js server on Azure?
Specifically:
var port = process.env.port
Most cloud providers in my experience don't let you play on other ports. You can always specify a localhost port too, though by doing this:
var port = process.env.port || 3001 //(or whatever)
app.listen(port);
this way if process.env.port is undefined (which it will be in your dev environment) you fallback to 3001.
Make sense?

Related

Prevent "EADDRINUSE" with pm2?

I am about to switch my node application server from phusion passenger to pm2.
Most of the ports of my apps are set to 3001. With passenger that's never been a problem, but with pm2 ports collide (EADDRINUSE).
Do I have to set a different port for every app to prevent port collisions?
Yes, of course you need to have each application listen on a different and free port. It is your app that listen to port, not PM2.
You can leave the same port in source code, but in this case, start your app like this to change port when starting your app:
// Work for express and some others
PORT=3012 pm2 start -n "My Application" app.js
That's because express add this in your starter script:
var port = normalizePort( process.env.PORT || '3509' );
Notice that other package may use another name env var, like NODE_PORT for example.

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.

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.

Heroku running a node module server

I am trying to create a node app that runs a module called noodlejs. This starts its own server running on port 8888 (on my local version). I have pushed the changes to heroku and no errors are caused. However how do I now access the noodlejs server on port 8888? Is this possible or does it need to run on another port?
Thanks!
I don't think you can run the app on port 8888 or any other port for that matter on Heroku. You can only choose between 80 or 443. And to do that, you use process.env.PORT environment variable that Heroku exposes.

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);

Resources