Node.js port issue on Heroku cedar stack - node.js

I'm running a basic Express app in Node.js and trying to deploy to Heroku. The app works fine locally and I believe my setup with Heroku has gone well up until starting the server where i get the following error:
2011-09-21T16:42:36+00:00 heroku[web.1]: State changed from created to starting
2011-09-21T16:42:39+00:00 app[web.1]: Express server listening on port 3000 in production mode
2011-09-21T16:42:40+00:00 heroku[web.1]: Error R11 (Bad bind) -> Process bound to port 3000, should be 12810 (see environment variable PORT)
2011-09-21T16:42:40+00:00 heroku[web.1]: Stopping process with SIGKILL
2011-09-21T16:42:40+00:00 heroku[web.1]: Process exited
this is currently all i have in my app.js
app.listen(3000);
I did also run this as mentioned on Heroku's getting started.
$ heroku config:add NODE_ENV=production
Adding config vars:
NODE_ENV => production
I believe I just need to set up the port for production? Thanks.

Can you show the entire section of code where you call listen? You should be checking for the process environment variable PORT, not just hardcoding it to 3000. From their docs:
var port = process.env.PORT || 3000;
app.listen(port, function() {

Related

Heroku + Node.js: Web process failed to bind to $PORT within 60 seconds of launch

I built a Node.js/Express Web Api. I want to deploy it on Heroku. It works fine on local but when I deploy it I get the following error:
Error R10 (Boot timeout) -> Web process failed to bind to $PORT within 60 seconds of launch
index.js
app.listen(process.env.PORT || 3000, () => {
console.log(`app running on ${process.env.PORT}`);
});
.env
PORT=3000
I have configured the PORT variable on Heroku -> Settings ->Config Vars section. However it didn't solve my problem.
Additionally, when I look up the application logs on Heroku, the callback function on the listen() method, outputs the following:
2022-07-15T20:06:19.268954+00:00 app[web.1]: app running on undefined
2022-07-15T20:06:20.230943+00:00 app[web.1]: connected to db
connected to db output came from mongodb connection. My app also gets the mongodb credentials from .env file and it works fine. But why I can't get PORT variable and it returns undefined?
This is a duplicate of this question and the answer is - PORT variable is provided by Heroku and you can't set it.
I removed the PORT variable either on .env file or Config Vars on Heroku.
And I change the way to call the PORT variable to not conflict with Heroku's default PORT.
index.js
let port = process.env.PORT || 3000;
app.listen(port, () => {
console.log(`app running on ${port} `);
});
App runs on 3000 port in localhost and Heroku's default port in server now.

"Error R10 (Boot timeout) -> Web process failed to bind to $PORT within 60 seconds of launch" on node.js deploy with Docker in Heroku

I'm trying to deploy an app to Heroku which was dockernized.
This is my Dockerfile:
FROM node:4.2.2
WORKDIR /usr/src
RUN git clone https://github.com/***.git
WORKDIR /usr/src/application
RUN ./install.sh
EXPOSE 80 3000
CMD bash -C '/usr/src/application/start.sh'
Also, I have a Profile with just one line as follow:
web: node bin/www
Following the steps on the documentation, I've pushed the image via two commands:
heroku container:push web --app immense-falls-39679 and released with
heroku container:release web --app immense-falls-39679.
In the logs, everything seems to be going ok, with the app deployed successfully. But when it is about to start (with the start.sh script) it crashes with the error
2018-10-23T20:47:44.025502+00:00 app[web.1]: [20:47:44] Finished 'build-app' after 7.11 s
2018-10-23T20:47:44.025958+00:00 app[web.1]: [20:47:44] Starting 'bundle-release'...
2018-10-23T20:48:07.932228+00:00 heroku[web.1]: Error R10 (Boot timeout) -> Web process failed to bind to $PORT within 60 seconds of launch
2018-10-23T20:48:07.932327+00:00 heroku[web.1]: Stopping process with SIGKILL
2018-10-23T20:48:08.058850+00:00 heroku[web.1]: Process exited with status 137
2018-10-23T20:48:08.097770+00:00 heroku[web.1]: State changed from starting to crashed
I've researched and most have said that this is due to fixed setting the PORT value. But, in my bin/www (where is my server configs), I've already set the port to be either a fixed value or from the process.env
var app = require('../out/app');
var debug = require('debug')('server:server');
var http = require('http');
/**
* Get port from environment and store in Express.
*/
app.set('port', (process.env.PORT || 3000));
/**
* Create HTTP server.
*/
var server = http.createServer(app);
/**
* Listen on provided port, on all network interfaces.
*/
server.listen(app.get('port'), function() {
listenForNotificationRequests();
});
server.on('error', onError);
server.on('listening', onListening);
I dont have any other clues on how fixing it. I would really appreciate any help. Thanks in advance
To deploy a docker container to Heroku you'll need to adjust your Dockerfile to work with Heroku.
Remove EXPOSE 80 3000, because it is not used by Heroku
When you deploy, the app must listen on $PORT set by Heroku. Your app can access this port with process.env.PORT. If you'd rather pass the port in the Dockerfile cmd then you can use CMD start_app_command -p $PORT
run heroku logs --tail after deploying and you will see that $PORT is replaced with the port set by Heroku.
https://devcenter.heroku.com/articles/container-registry-and-runtime

Heroku Node app keeps crashing

I have an Express app hosted on Heroku. Locally it runs just fine, but keeps crashing on Heroku. Every time I go to it I get the 'Application Error' page.
I double checked all environment variables and they seem ok.
The Heroku logs don't exactly offer much help:
2017-04-16T23:02:49.001768+00:00 heroku[web.1]: State changed from crashed
to starting
2017-04-16T23:02:52.072906+00:00 heroku[web.1]: Starting process with
command `npm run start`
2017-04-16T23:02:56.042611+00:00 app[web.1]:
2017-04-16T23:02:56.042628+00:00 app[web.1]: > my-app#0.0.1 start /app
2017-04-16T23:02:56.042629+00:00 app[web.1]: > node bin/app
2017-04-16T23:02:56.042630+00:00 app[web.1]:
2017-04-16T23:02:57.200912+00:00 heroku[web.1]: State changed from starting
to crashed
2017-04-16T23:02:57.201454+00:00 heroku[web.1]: State changed from crashed
to starting
2017-04-16T23:02:57.192198+00:00 heroku[web.1]: Process exited with status 0
2017-04-16T23:03:00.981528+00:00 heroku[web.1]: Starting process with
command `npm run start`
2017-04-16T23:03:04.312475+00:00 app[web.1]:
2017-04-16T23:03:04.312489+00:00 app[web.1]: > my-app#0.0.1 start /app
2017-04-16T23:03:04.312490+00:00 app[web.1]: > node bin/app
2017-04-16T23:03:04.312491+00:00 app[web.1]:
2017-04-16T23:03:06.093211+00:00 heroku[web.1]: Process exited with status 0
2017-04-16T23:03:06.112842+00:00 heroku[web.1]: State changed from starting
to crashed
So then I tried starting the app manually:
$ heroku run bash
...
$ npm run start
I got no errors. I'm using the pm2 process manager, so I checked it to see if the app is running and it seems like it is:
$ node_modules/.bin/pm2 list
...
But then when I go to open my app I get the same 'Application error' page as before.
'heroku restart' doesn't help. It just crashes again every time.
The only difference between my local environment and Heroku (as far as I can tell at least...) is that locally I have a file from which I'm reading a RSA private key, and on Heroku that key is read from the environment.
Edit: As requested, here's my Procfile:
web: npm run start
Any ideas?
Your app need to start listening to the port defined in the env. For example if you have an expressjs app it should look something like this:
const express = require('express')
const app = express()
const port = process.env.PORT || 3000
app.get('/', (req, res) => res.send("Hello world"))
app.listen(port, () => console.log(`Example app listening on port ${port}!`))
If the app does not bind to the correct port, heroku is going to consider the app unhealthy and will terminate it.
You need to see this line in the logs, which indicates that heroku recognized the app as healthy:
State changed from starting to up
Keep in mind that this port thing is a special requirement for the web process. The other processes in your app does not have such a requirement.
Try 2 things:
Change your procfile:
web: node start.js
Then make sure that your web process it running by scaling it up:
heroku ps:scale web=1
On your computer in a terminal window run this command in your project:
heroku local web
It simulates how Heroku runs your app and should give you information of what is happening.

My socket.io app's server is not starting on Heroku

I'm trying to deploy my app on Heroku. I've followed these instructions: https://devcenter.heroku.com/articles/getting-started-with-nodejs, enables websockets, but when I run $heroku open, the I land on the error page.
Here are the logs I get from heroku:
2014-07-14T11:41:27.331343+00:00 heroku[web.1]: Starting process with command `node index.js`
2014-07-14T11:41:28.431660+00:00 app[web.1]: info: socket.io started
2014-07-14T11:42:27.710153+00:00 heroku[web.1]: Error R10 (Boot timeout) -> Web process failed to bind to $PORT within 60 seconds of launch
2014-07-14T11:42:27.710206+00:00 heroku[web.1]: Stopping process with SIGKILL
2014-07-14T11:42:28.915226+00:00 heroku[web.1]: Process exited with status 137
2014-07-14T11:42:28.927884+00:00 heroku[web.1]: State changed from starting to crashed
Here is the index.js file of my app:
// Import the Express module
var express = require('express');
// Import the 'path' module (packaged with Node.js)
var path = require('path');
// Create a new instance of Express
var app = express();
// Import Anagrammatix game file.
var agx = require('./game');
// Create a simple Express application
app.configure(function() {
// Turn down the logging activity
app.use(express.logger('dev'));
// Serve static html, js, css, and image files from the 'public' directory
app.use(express.static(path.join(__dirname,'public')));
});
// Create a Node.js based http server on port 8080
var server = require('http').createServer(app).listen(8080);
// Create a Socket.IO server and attach it to the http server
var io = require('socket.io').listen(server);
// Reduce the logging output of Socket.IO
io.set('log level',1);
// Listen for Socket.IO Connections. Once connected, start the game logic.
io.sockets.on('connection', function (socket) {
//console.log('client connected');
agx.initGame(io, socket);
});
The node_modules I'm using are:
- express
- mysql
- socket.io
Do you know what I should change to get the app work? Let me know if you need me to provide more info.
Thanks!
Your code is trying to bind to a hard-coded port 8080, but Heroku will tell your app which port to connect to. Heroku uses this to detect that your app's started correctly, as well as for routing. Your code's not starting on that port, though, so it gets killed with the error "Web process failed to bind to $PORT within 60 seconds of launch". Heroku doesn't know that your app has started, and it doesn't know which port it's listening to.
It's an easy fix, though. This will tell your app to connect to the port specified in the PORT environment variable, or 8080 if that variable isn't set:
var server = require('http').createServer(app).listen(process.env.PORT || 8080);

Nodejs port error with Heroku

I am trying to push my app to heroku bu I am getting this problem where I am using the correct port number using the process.env.PORT variable but still I am getting this error message:
heroku[web.1]: Starting process with command `node app.js`
app[web.1]: info - socket.io started
app[web.1]: Express server listening on port 49559 in development mode
heroku[web.1]: Error R11 (Bad bind) -> Process bound to port 10843, should be 49559 (see environment variable PORT)
heroku[web.1]: Stopping process with SIGKILL
heroku[web.1]: Process exited
You can see in this error message that app is using the right port but still heroku shows the bad bind error. Any help is appreciated.
Heroku will pass the port in a environment variable named PORT, which means you can access it via process.env.
Simply change your code to call listen with the proper port
var port = process.env.PORT || 3000;
app.listen(port);
I'm sure you are using flash sockets that use port 10843, and that port is not the one assigned to the app, you need to set:
io.configure(function () {
io.set('transports', ['xhr-polling']);
io.set("polling duration", 10);
});

Resources