Port Timeout With NodeJS/Webpack Heroku Deployment - node.js

Note: Everything works perfectly on my localhost environment. When I git push heroku master I get a successful push/deploy. I check the Heroku Logs and I see this error:
[]: Stopping process with SIGKILL
[]: Error R10 (Boot timeout) -> Web process failed to bind to $PORT within 120 seconds of launch
I have read many posts regarding this issue and the fix that I see is
app.set('port', (process.env.PORT || 4000));
This is not my fix since I am already doing this in my server code. Here is my server code:
var path = require('path');
var express = require('express');
var app = express();
app.set('port', (process.env.PORT || 4000));
app.use(express.static(__dirname + '/public'));
app.get('*', function response(req, res) {
res.render(path.join(__dirname, 'public/index.html'));
});
app.listen(app.get('port'), 'localhost', function onStart(err) {
if (err) {
console.log(err);
}
console.info('==> Listening on port %s.', app.get('port'));
});
NOTE: In the Heroku logs, ==> Listening on port %s. is printed and THEN the timeout happens after that (with no error). So it does get to the end of my server code without error and it prints the correct, random Heroku port.
Furthermore, webpack -p also creates my bundle.js correctly too.
My package.json has these two commands and they are executed without error:
"start": "NODE_ENV=production webpack -p && node server",
"postinstall": "bower install --force"
I am truly at a loss. Please help!
EDIT: I believe I have truly isolated the problem to my server.js file. I ended up doing webpack -p and pushed up the bundle.js file––essentially bypassing webpack on Heroku. I then do the simple npm start command (which is essentially just node server.js command) and my app still times out when trying to connect to the port.

WOW I figured it out. I had this line in my server:
app.listen(app.get('port'), 'localhost', function onStart(err) ....
I just needed to remove 'localhost' from the listen() function.

Related

Heroku R10 (Boot timeout) - Node.js

So I have got into this problem while deploying my website with heroku. I have seen other solutions in which I have tried to change the scripts in package.json and Procfile aswell but no luck for me Please help me. This website runs well locally
Here is my index.js main function
app.get("/", function(req, res){
const fact = facts.space
res.render("home", {fact: fact});
});
Here is my listen function.
const port = 3000 || process.env.PORT
app.listen(port, function () {
console.log("# working");
})
Here is my packages.json
packages.json
Here is my Procfile
Procfile
Here is the error that I am getting
Error
Here are all of my files All files
You need to invert the definition of the port to check first the $PORT env variable, then fall back to the default port number (3000) in case it does not exist (ie on local development environment)
const port = process.env.PORT || 3000

Deploying first app to Heroku: Error R10 (Boot timeout) -> Web process failed to bind to $PORT within 60 seconds of launch

I am trying to deploy an Express/React/Mongo app to Heroku for the first time.
It is failing with a 503 error. The logs states the following:
Error R10 (Boot timeout) -> Web process failed to bind to $PORT within
60 seconds of launch
I have set the following port variable in the root index.js file:
const port = process.env.PORT || 3000;
and use it here:
app.listen(port, function(){
console.log("Express server is running on port " + port)
})
It outputs "Express server is running on port 3000", which suggest that it is not picking up the Environmental variable.
I have been trying to follow instructions here: https://coursework.vschool.io/deploying-mern-with-heroku/
The key part that I am may be misunderstanding:
With Heroku, you need to set the environment variables on your newly
created Heroku app so it knows which values to use when the project is
deployed. You can do this two ways, either online on Heroku's website,
or through the command line using the heroku CLI. Since we are not
creating a new Heroku remote repository, all environment variables
will need to be added using Heroku.com.
I took this to mean that I should set an environmental variable Heroku.com, which I believe I have done so:
What am I failing to grok?
EDIT: I have tried setting theprocess.env.PORT=8000 from the Heroku CLI:
heroku config:set process.env.PORT=8000
But get the following error:
» Error: Missing required flag: » -a, --app APP app to run
command against » See more help with --help
Here is the full index.js:
const express = require('express'),
cors = require('cors'),
app = express(),
port = process.env.PORT || 3000;
bodyParser = require('body-parser'),
todoRoutes = require('./routes/todo'),
path = require("path");
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: true}));
app.use(cors());
app.use(express.static(path.join(__dirname, "client", "build")))
app.get('/', function (req, res){
res.send('Root route')
})
app.use('/api/todos', todoRoutes);
app.get("*", (req, res) => {
res.sendFile(path.join(__dirname, "client", "build", "index.html"));
});
app.listen(port, function(){
console.log("Express server is running on port " + port)
})
I had local dev server running whilst deploying to Heroku. Tried redeploying after stopping dev server, and it worked. Local servers interfered with environment variables in deployment.

Say 'Hello World!' in Openshift with Node.js

I created an app in Openshift and created a local git repo on my computer. I want to change the default welcome page here: http://nodejs-j4nos.rhcloud.com:3000 and just tell Hello world as this tutorial say.
So I removed from local repo the index.html, and modified server.js, pasted in this code below. And commit, and push. I get a long approval, that they accepted my commit.
If I good understand I do not have to stop node and start it again, but Openshift do it for me. But as you can see no Hello World is able to see, when open link in browser (http://nodejs-j4nos.rhcloud.com:3000) why?
var express = require('express');
var app = express();
app.get('/', function (req, res) {
res.send('Hello World!');
});
var server = app.listen(3000, function () {
var host = server.address().address;
var port = server.address().port;
console.log('Example app listening at http://%s:%s', host, port);
});
remote: Git Post-Receive Result: success
remote: Activation status: success
remote: Deployment completed with status: success
To ssh://5556b4c4fcf9336abf0000de#nodejs-j4nos.rhcloud.com/~/git/nodejs.git/
and here is the tree structure, express is listed
Based on this SO answer I tried to modify script, but does not helped:
var express = require('express');
var app = express();
app.set('port', process.env.OPENSHIFT_NODEJS_PORT || 8080);
app.set('ip', process.env.OPENSHIFT_NODEJS_IP || '127.0.0.1');
http.createServer(app).listen(app.get('port'), app.get('ip'), function(){
console.log('Express server listening on port ' + app.get('port'));
});
app.get('/', function (req, res) {
res.send('Hello World!');
});
Link is working now: http://nodejs-j4nos.rhcloud.com The right script to show "Hello world!" is
var http = require('http');
var express = require('express');
var app = express();
app.set('port', process.env.OPENSHIFT_NODEJS_PORT || 8080);
app.set('ip', process.env.OPENSHIFT_NODEJS_IP || '127.0.0.1');
http.createServer(app).listen(app.get('port'), app.get('ip'), function(){
console.log('Express server listening on port ' + app.get('port'));
});
app.get('/', function (req, res) {
res.send('Hello World!');
});
And here is the proof:
Thanks for this: Deployed Node app to OpenShift successfully, OpenShift still shows default page
and this question: Node.js Deployment in openshift
And you should now that you can write in /app-root/repo the $ node server.js command, and if something wrong with script, it will indicate right there
I´ve readed this, maybe you must use a port over 15000:
https://help.openshift.com/hc/en-us/articles/202185874-I-can-t-bind-to-a-port
Found my notes on using OpenShift with Node:
The openshift system has some integrated control tools to support its ‘gear’ system, useful to control the openshift
application and environment.
gear control your application (start, stop, restart, etc)
or deps with --cart (gear start --cart mysql-5.1)
tail_all tail all log files. This command displays the last entries
in your log files as they are written. You can hit
<ctrl>-c to exit this command.
export list available environment variables
rm remove files / directories
ls list files / directories
ps list running applications
kill kill running applications
mysql interactive MySQL shell
mongo interactive MongoDB shell
psql interactive PostgreSQL shell
quota list disk usage
The gear system has additional commands. OpenShift Gear Control, An assortment of gear utilities:
COMMANDS:
build Run the build steps
deploy Run the deploy steps
help Display global or [command] help documentation.
postreceive Run the git postreceive steps
prereceive Run the git prereceive steps
reload Reload a cart
remotedeploy Run the remotedeploy steps
restart Restart a cart
restore Restore an application
snapshot Snapshot an application
start Start the gear/cart
status Get the status for a cart
stop Stop the gear/cart
Will any of this stuff help you stop and restart the gear? I'd start with the simple 'gear' command. I don't remember, is Express loaded up via NPM or now native with node? At one time it was a NPM install. Those don't get pushed to Openshift.
What is the directory tree structure on the openshift nodejs server?
root
\ app-root
\ data
\ repo <- - the working files for web content end up here.
\ runtime
\ git
\ nodejs
In openshift dependencies don't get pushed. For that you can login thru ssh and go to:
cd app-root/repo or cd $OPENSHIFT_REPO_DIR and then npm install tool_of_choice

NodeJS Deployment confusion between localhost and other domain?

I have a simple program which executes fine in localhost.
var express = require('express');
var app = express();
app.get('/', function (req, res) {
res.send('Hello World!');
});
var port = process.env.PORT || 8080;
var host = "127.0.0.1";
var server = app.listen(port, host, function(){
console.log("Server running in : ",host ," with port no : ",port);
});
Trying to deploy the same to heroku using codeship. Everything is building perfect except the last line of deployment test command i.e node index.js which in turn is referring to 127.0.0.1 and stops deploying. May i know do i need to change something here for the host and port address
Just don't provide a host:
var server = app.listen(port, function() {
console.log('Server listening on', port);
});
(This implies, "accept connections on any host, on this port", vs what you're trying which implies, "accept connections on 127.0.0.1 on this port")
Try to run your app on localhost with the help of foreman that is a part of the Heroku Toolbelt. For instance:
foreman start web
You should see your app running on http://localhost:5000 or the port you have specified in your package.json file.
Suggest this link for further queries:
prerequisites to deploy a node app on Heroku?
I was able to host it successfully following through this steps
As suggested by #hunterloftis, i removed hostname.
More importantly, Procfile was missing,so added it and deployed successfully

How to deploy a reveal.js app to heroku?

I am trying to deploy a reveal.js application to Heroku. Reveal.js runs on node via grunt connect command. The app also requires ruby for compiling assets on-the-fly. Locally, I can run the app by using grunt serve.
Initially, because of compass being a dependency of grunt watch, Heroku only detected the Gemfile and assumed I was running a ruby app. I used the nodejs custom buildpack to force Heroku to see it as a nodejs app.
Procfile contains
web: grunt serve
Log shows
2013-06-17T13:51:56.187012+00:00 heroku[router]: at=error code=H14 desc="No web processes running"
heroku ps shows nothing either. I can run "heroku run grunt serve" successfully, and I have modified the default Gruntfile.js that comes with reveal to accept process.env i.e.
connect: {
server: {
options: {
port: process.env.PORT || 8000,
base: '.'
}
}
}
As a last attempt, I tried using the Heroku-nodejs-grunt build pack (https://github.com/mbuchetics/heroku-buildpack-nodejs-grunt) which will run a grunt task on deploy to compile assets. Still no luck, heroku logs --tail still shows no process running. Exploring with heroku run reveals that grunt is available, and the grunt serve command successfully executes.
When starting to use the new grunt build pack I got an error with the above Gruntfile.js saying "process" is undefined. I switched the port to 0.
The port on which the webserver will respond. The task will fail if
the specified port is already in use. You can use the special values 0
or '?' to use a system-assigned port.
Didn't work, tried "?", didn't work (still no web process and heroku restart doesn't do anything)
I can't figure out how to get Heroku to use grunt serve as my main web server process!
I was able to make it work using nodejs and expressJs.
By following the heroku "getting started with nodejs", I was able to get a working webapp with expressjs and by declaring this in the web.js:
var express = require("express");
var app = express();
app.use(express.logger());
app.use("/", express.static(__dirname));
var port = process.env.PORT || 5000;
app.listen(port, function() {
console.log("Listening on " + port);
});
With this you serve everything from / statically.
You have the sources here: https://github.com/MichaelBitard/revealjs_heroku and a working example here: http://murmuring-cove-4212.herokuapp.com/
Your problem was that by default grunt serve binds to localhost. For it to work you will need to do a couple of small changes to reveal.js:
First add grunt-cli as a devDependency:
diff --git a/package.json b/package.json
index 10489bb..4c58442 100644
--- a/package.json
+++ b/package.json
## -36,6 +36,7 ##
"grunt-contrib-connect": "~0.8.0",
"grunt-autoprefixer": "~1.0.1",
"grunt-zip": "~0.7.0",
+ "grunt-cli": "~0.1.13",
"grunt": "~0.4.0",
"node-sass": "~0.9.3"
},
Then add a hostname parameter to grunt that will be used to bind to 0.0.0.0 instead of localhost.
diff --git a/Gruntfile.js b/Gruntfile.js
index 3e67b9f..b2bfc47 100644
--- a/Gruntfile.js
+++ b/Gruntfile.js
## -1,5 +1,6 ##
/* global module:false */
module.exports = function(grunt) {
+ var hostname = grunt.option('hostname') || 'localhost';
var port = grunt.option('port') || 8000;
// Project configuration
grunt.initConfig({
## -94,6 +95,7 ## module.exports = function(grunt) {
connect: {
server: {
options: {
+ hostname: hostname,
port: port,
base: '.',
livereload: true,
Now you can create a Procfile with the following contents to deploy to Heroku:
web: npm install && node_modules/.bin/grunt serve --hostname 0.0.0.0 --port $PORT
I have created a PR for the needed changes to reveal.js.
Currently with express v~4.13.3 express.logger() is deprecated and is not included with the express package. To solve this I had to import the dependency morgan.
My web.js file ended up being the following:
var express = require('express');
var morgan = require('morgan');
var app = express();
var port = process.env.PORT || 5000;
app.use(morgan('combined'));
app.use('/', express.static(__dirname));
app.listen(port, function() {
console.log('Server started on ' + port);
});
As well, I needed to update my package.json to include the morgan lib. My dependencies in the file works with:
...
"dependencies": {
"express": "~4.13.3",
"morgan": "~1.7.0",
"grunt-cli": "~0.1.13",
"mustache": "~2.2.1",
"socket.io": "~1.3.7"
},
...

Resources