Having trouble deploying heroku app: application fails to bind to $port - node.js

I followed the procedures of the instructor , but the page is giving me an error. can someone help me here?
Hyper Terminal Application Error heroku logs

The application uses the default port 3000 which won't work on Heroku: the application needs to bind to the port provided in the PORT env variable
const PORT = process.env.PORT || 3000;

Related

Heroku Node.JS App Not Deploying

So this is my first time using Heroku to deploy a node app. I have been encountering this issue:
I have no idea what is going on I have index file in the root folder and my localhost port is 5000. Any help would be greatly be appreciated.
Structure of Files
Package.json
Heroku doesn't use a static $PORT like the way you run your node app locally, you will have to make your $PORT dynamic port to enable heroku start the application listen on that any available $PORT
Change your $PORT to this:
// process.env.PORT lets the port be set by Heroku
var port = process.env.PORT || 5000;

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.

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

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?

Resources