I am using nodeJS, express, swagger-tools to get Swagger API document showing from /docs in the my Heroku dyno. Locally this works, but on Heroku the port is not listening.
app[web.1]: info: Swagger-ui is available on localhost:5765/docs
heroku[web.1]: State changed from crashed to starting
heroku[web.1]: Error R10 (Boot timeout) -> Web process failed to bind to $PORT within 60 seconds of launch
heroku[web.1]: Stopping process with SIGKILL
heroku[web.1]: Process exited with status 137
On the code, I have the port predetermined by Heroku.
var listen_addr = 'localhost';
var listen_port = process.env.PORT || 8080;
var swaggerDoc = require(options.swaggerUi);
swaggerDoc.host = listen_addr +":" + listen_port; // making sure of add
swaggerTools.initializeMiddleware(swaggerDoc, function (middleware) {
app.use(middleware.swaggerMetadata());
app.use(middleware.swaggerValidator());
app.use(middleware.swaggerUi() );
app.use(middleware.swaggerRouter(options) );
app.listen(listen_port, listen_addr) {
winston.info('Server is listening on %s:%d', listen, port);
winston.info('Swagger-ui is available on %s:%d/docs', listen, port);
});
});
UPDATE: the problem seemed to be solved by changing app.listen to app.listen( process.env.PORT || 3000, function()
Related
My nodejs app is crashing in Heroku. I have two server running in two different ports. One for express routes and another for socket.io. Apps builds fine and deployed in Heroku. It starts, DB connection is OK, then a server daemon also works fine for some period. Then it change state to crashed. No error log after the crash and no reason for crash. Here is log from Heroku,
2020-04-22T14:52:19.980682+00:00 app[web.1]: > iLearn#1.0.0 start /app
2020-04-22T14:52:19.980683+00:00 app[web.1]: > PORT=5000 node server.js
2020-04-22T14:52:19.980683+00:00 app[web.1]:
2020-04-22T14:52:20.741515+00:00 app[web.1]:
2020-04-22T14:52:20.782048+00:00 app[web.1]: API server started on: 5000
2020-04-22T14:52:21.087402+00:00 app[web.1]: Messaging Manager started..
2020-04-22T14:52:21.087501+00:00 app[web.1]: MessagingManager::startDaemon
2020-04-22T14:52:21.089358+00:00 app[web.1]: Messaging server running on port:5001
2020-04-22T14:52:21.130079+00:00 app[web.1]: DB Connected
2020-04-22T14:52:21.132905+00:00 app[web.1]: {"timestamp":"2020-04-22T14:52:21.131Z","message":"DB Connected","level":"info"}
2020-04-22T14:52:26.097164+00:00 app[web.1]: MessagingManager::daemon
2020-04-22T14:52:31.100561+00:00 app[web.1]: MessagingManager::daemon
2020-04-22T14:52:36.107406+00:00 app[web.1]: MessagingManager::daemon
2020-04-22T14:52:41.112479+00:00 app[web.1]: MessagingManager::daemon
2020-04-22T14:52:46.116481+00:00 app[web.1]: MessagingManager::daemon
2020-04-22T14:52:51.121785+00:00 app[web.1]: MessagingManager::daemon
2020-04-22T14:52:56.127394+00:00 app[web.1]: MessagingManager::daemon
2020-04-22T14:53:01.136499+00:00 app[web.1]: MessagingManager::daemon
2020-04-22T14:53:06.136680+00:00 app[web.1]: MessagingManager::daemon
2020-04-22T14:53:11.144029+00:00 app[web.1]: MessagingManager::daemon
2020-04-22T14:53:16.145616+00:00 app[web.1]: MessagingManager::daemon
2020-04-22T14:53:16.792389+00:00 heroku[web.1]: State changed from starting to crashed
Here is my server.js file,
const express = require('express')
const MessagingManager = require("./util/messagingManager")
app = express()
bodyParser = require('body-parser');
require('dotenv').config();
port = process.env.PORT || 5000;
app.listen(port);
console.log('API server started on: ' + port);
app.use(express.static('public'))
//app.use(formidable());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
app.set('view engine', 'pug');
app.set('views', './views')
var routes = require('./routes'); //importing route
app.use('/', routes); //register the route
const messagingApp = express();
const messagingServer = require("http").createServer(messagingApp);
const io = require("socket.io").listen(messagingServer);
let messagingManager = new MessagingManager()
messagingManager.startDaemon()
port = process.env.MESSAGING_PORT || 5001;
io.on("connection", socket => {
console.log("a user connected");
let id = socket.handshake.query.id
console.log(socket.id)
messagingManager.addConnection(id, socket)
socket.on("message", msg => {
if (msg.type == "MESSAGE_READ_STATUS")
messagingManager.setMessageStatus(msg)
else
messagingManager.sendMessage(msg)
});
socket.on("disconnect", () => {
//io.emit("chat message", msg);
});
socket.on("endsession", msg => {
//io.emit("chat message", msg);
console.log("session ended." + id)
messagingManager.removeConnection(msg.id)
socket.disconnect()
});
// not used
socket.on("session", msg => {
//io.emit("chat message", msg);
console.log("session established")
messagingManager.addConnection(msg.id, socket)
});
});
messagingServer.listen(port, () => console.log("Messaging server running on port:" + port));
It's hard to actually to know why the application crashed as log just says, application crashed without any message. Port binding error was not thrown and app listen callback function is called. Actual issue seems to be port. I have set port in both .env and package.json file. It overrides heroku dynamic port. Thanks #Beppe C to pointing out this. I deleted port variable from both .env and package.json and redeploy the app. Now it is working fine. Another issue is heroku app does not allow two port in the same application. So, I have to create two app. One for express HTTP route and another for socket.io. Hope it helps someone.
The code shown below fails on Heroku with the error Stopping process with SIGKILL
http.createServer((req, res) => {
var server_port = process.env.YOUR_PORT || process.env.PORT || 80;
var server_host = process.env.YOUR_HOST || '0.0.0.0';
server.listen(server_port, server_host, function() {
console.log('Listening on port %d', server_port);
});
});
Logs:
heroku[web.1]: Error R10 (Boot timeout) -> Web process failed to bind to $PORT within 60 seconds of launch
How can I fix this issue?
You have not declared the variable server.
You can do:
var server = http.createServer((req, res) => {
//your stuff
});
server.listen(process.env.PORT || 80, () => {
console.log("Listening on port 80");
});
I'm getting this error in production when my app is rebooting after a push. It started happening after I added the worker to my Procfile.
web: node server.js
worker: node bin/jobWorker/
My express server is as such:
app.get('*', (req, res) => {
res
.status(200)
.render('index', {
build: isDev ? null : '/build',
});
});
server
.listen(
Config.port,
Config.env !== 'production' ? Config.host : null,
(err) => logServerConfig(err)
);
Any ideas why this error is showing up? I've looked around for answers and none of the solutions applied to this scenario. Most have said to not enter a static port, which is not what I'm doing. I grab the port from process.env.PORT where Heroku populates it.
Thanks for any help!
I am attempting to get my Node.js app (which runs perfectly fine on localhost) running on Heroku, but running into some issues. There have been a couple other questions answered with the same error, but none of the solutions have worked for me. Here is what I'm getting from the Heroku logs:
2016-12-28T22:06:45.705675+00:00 heroku[web.1]: Starting process with command `node app.js`
2016-12-28T22:06:48.358120+00:00 app[web.1]: Environment: HEROKU
2016-12-28T22:06:48.360021+00:00 app[web.1]: Listening on PORT:13812
2016-12-28T22:06:49.226997+00:00 app[web.1]: Application Loaded...
2016-12-28T22:07:45.958705+00:00 heroku[web.1]: Error R10 (Boot timeout) -> Web process failed to bind to $PORT within 60 seconds of launch
2016-12-28T22:07:45.958927+00:00 heroku[web.1]: Stopping process with SIGKILL
2016-12-28T22:07:46.102423+00:00 heroku[web.1]: Process exited with status 137
2016-12-28T22:07:46.079295+00:00 heroku[web.1]: State changed from starting to crashed
2016-12-28T22:07:46.080349+00:00 heroku[web.1]: State changed from crashed to starting
Rinse and repeat. Here is what I get when I try to navigate to my URL after my application has started:
2016-12-28T22:16:57.209624+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=sc-library-test.herokuapp.com request_id=e98196c1-1545-4a49-9c20-f4ac7f2ab964 fwd="70.95.172.183" dyno= connect= service= status=503 bytes=
Now for my code, particularly the bin/www file that comes with Express.
const config = require('../config');
/**
* Get port from environment and store in Express.
*/
const port = normalizePort(config.port || '3000');
app.set('port', port);
/**
* Get hostname from environment.
*/
const host = config.host || 'localhost';
/**
* Create HTTP server.
*/
const server = http.createServer(app);
/**
* Listen on provided port, on all network interfaces.
*/
server.listen(port, host);
function normalizePort(val) {
console.log("val: " + val);
const port = parseInt(val, 10);
console.log(port);
if (isNaN(port)) {
// named pipe
console.log("nan");
return val;
}
if (port >= 0) {
// port number
console.log(">=0");
return port;
}
console.log("false");
return false;
}
I've tried doing the server.listen with and without the host, but I get the same error either way. Here is my config file:
module.exports = {
port: process.env.PORT,
host: '0.0.0.0',
base_url: process.env.BASE_URL,
auth: {
client_id: process.env.CLIENT_ID
},
neo4j_href : process.env.NEO4J_HREF,
redis_url: process.env.REDIS_URL,
redis_secret: process.env.REDIS_SECRET
}
As you can see I'm pulling the port from my heroku environment, along with the rest of my environment variables, and using it on my server.listen().
Some other information... I'm not currently using a Procfile. My app consists of a single web server, and since my app is running with the single web dyno and no workers without the Procfile, I assume that's fine. Correct me if I'm wrong.
EDIT: Here is my updated bin/www file, which is still giving me the same error.
const app = require('../app');
const config = require('../config');
const port = config.port || '3000';
app.listen(port);
Your node app isn't actually starting the web server correctly. It appears to me that your usage of server.listen is incorrect (see the docs here).
While you could always rewrite your server.listen call to pass in the correct object -- since you're using Express, there's actually a much easier way. Express ships with support for running the server using a convenient wrapper around the standard library stuff. Here's how you can re-write your code to use it:
const config = require('../config');
const port = config.port || '3000';
app.listen(port);
This will start the server, bind it to 0.0.0.0, and make it listen on the desired port.
NOTE: you no longer need to parse the port into an integer, as this is handled by the abstraction layer.
$ foreman start
creates a link with my working app
I have a file Called Procfile that contains:
web: node server.js
Here is my package.json
{
"name": "node-example",
"version": "0.0.1",
"dependencies": {
"express": "3.1.0",
"jade": "*",
"socket.io": "*",
"underscore": "*"
}
}
So I go to start the "dynos" with
$ heroku ps:scale web=1
Scaling web dynos... done, now running 1
$ heroku ps
=== web (1X): `node server.js`
web.1: restarting 2013/09/27 22:10:44 (~ 34s ago)
a little later
$ heroku ps
=== web (1X): `node server.js`
web.1: crashed 2013/09/27 22:11:49 (~ 10s ago)
Any ideas on where I go from here?
EDIT: Here are some logs
2013-09-27T12:10:47.177359+00:00 heroku[web.1]: Starting process with command `node server.js`
2013-09-27T12:10:48.381526+00:00 app[web.1]: Server up and running. Go to http://127.0.0.1:8080
2013-09-27T12:10:48.342939+00:00 app[web.1]: info: socket.io started
2013-09-27T12:11:48.499022+00:00 heroku[web.1]: Error R10 (Boot timeout) -> Web process failed to bind to $PORT within 60 seconds of launch
2013-09-27T12:11:48.499325+00:00 heroku[web.1]: Stopping process with SIGKILL
2013-09-27T12:11:49.656186+00:00 heroku[web.1]: State changed from starting to crashed
2013-09-27T12:11:49.643225+00:00 heroku[web.1]: Process exited with status 137
EDI: server.js
var portx = process.env.PORT; // This is my addition
var express = require("express")
, app = express()
, http = require("http").createServer(app)
, io = require("socket.io").listen(http)
, _ = require("underscore");
var participants = []
app.set("ipaddr", "127.0.0.1");
app.set("port", portx); // to here
app.set("views", __dirname + "/views");
app.set("view engine", "jade");
app.use(express.static("public", __dirname + "/public"));
app.use(express.bodyParser());
app.get("/", function(request, response) {
response.render("index");
});
app.post("/message", function(request, response) {
var message = request.body.message;
if(_.isUndefined(message) || _.isEmpty(message.trim())) {
return response.json(400, {error: "Message is invalid"});
}
var name = request.body.name;
io.sockets.emit("incomingMessage", {message: message, name: name});
response.json(200, {message: "Message received"});
});
io.on("connection", function(socket){
socket.on("newUser", function(data) {
participants.push({id: data.id, name: data.name});
io.sockets.emit("newConnection", {participants: participants});
});
socket.on("nameChange", function(data) {
_.findWhere(participants, {id: socket.id}).name = data.name;
io.sockets.emit("nameChanged", {id: data.id, name: data.name});
});
socket.on("disconnect", function() {
participants = _.without(participants,_.findWhere(participants, {id: socket.id}));
io.sockets.emit("userDisconnected", {id: socket.id, sender:"system"});
});
});
http.listen(app.get("port"), app.get("ipaddr"), function() {
console.log("Server up and running. Go to http://" + app.get("ipaddr") + ":" + app.get("port"));
});
Now I get this similar error.
2013-09-28T02:38:51.301568+00:00 app[web.1]: info: socket.io started
2013-09-28T02:38:51.363825+00:00 app[web.1]: Server up and running. Go to http://127.0.0.1:31707
2013-09-28T02:39:50.974347+00:00 heroku[web.1]: Error R10 (Boot timeout) -> Web process failed to bind to $PORT within 60 seconds of launch
2013-09-28T02:39:50.974557+00:00 heroku[web.1]: Stopping process with SIGKILL
2013-09-28T02:39:52.927176+00:00 heroku[web.1]: Process exited with status 137
2013-09-28T02:39:52.945829+00:00 heroku[web.1]: State changed from starting to crashed
I can't be sure without the Node code where you create the server, however from the logs it looks like you are not listening on the correct port.
Heroku assigns your application a port (which is set as the "PORT" environment variable) that your application must listen on. If you do not connect to this port within 60s, it will kill your app, and this seems to be what is happening.
UPDATE:
In fact, based on your web.1 log entry, it looks like you are trying to listen on port 8080. I think it is highly unlikely that this is the port provided by Heroku. You can get the port Heroku provides from process.env.PORT, and then listen on that.
try looking in your dashboard check that you have at least one Dynos. It's happen to me that I add more than one app and I use the free trail so it keep moving the Dyno between the app.
Good Luck.