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

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

Related

Heroku deployment error: Web process failed to bind to $PORT within 60 seconds of launch [duplicate]

I'm developing an API on NodeJS using express and I'm getting this error while trying to deploy it to Heroku:
Error R10 (Boot timeout) -> Web process failed to bind to $PORT within 60 seconds of launch
I am using the process.env.PORT variable, as I found here on SO, but it is still not working. Here's the index.js code:
var express = require('express');
var app = express();
var bodyParser = require("body-parser");
app.use(bodyParser.json());
var router = require("./router");
app.use('/viatges', router);
app.listen(3000 || process.env.PORT,function(){
console.log("up and running on port "+process.env.PORT);
});
Do you have any idea of what could be causing this issue?
On the heroku logs i see this line
2017-04-01T11:44:07.091181+00:00 app[web.1]: up and running on port 27583
so I assume the PORT enviornment variable is correctly set up...
Nevermind, i just fixed it. I changed the order of the operands in the OR, I assume javascript just went with the first as it was defined.
It now looks like this:
app.listen(process.env.PORT || 3000 ,function(){
console.log("up and running on port "+process.env.PORT);
});

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

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

Node.js port issue on Heroku cedar stack

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() {

Resources