Heroku R10 (Boot timeout) - Node.js - 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

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.

A deployed Heroku app works online only on the PC I deployed it from

I finished my MERN stack app and deployed it to Heroku. Opened it on Heroku and it worked. Funny thing is, it doesn't work on any other machine other than the one I deployed it from.
When opened on any other machine, it just goes to blank white screen with the following error in console:
redux.js:575 Uncaught TypeError: Cannot read property 'apply' of undefined
at redux.js:575
at u (redux.js:79)
at Module.102 (store.js:9)
at f ((index):1)
at Object.57 (spinner.gif:1)
at f ((index):1)
at a ((index):1)
at Array.e [as push] ((index):1)
at main.4febefcb.chunk.js:1
I am trying to figure out what are the possible causes of this behavior.
I've already tried creating a new Heroku app and re-deploying it, restarting dynos and checking my server.js config for ports.
//static assets for production
if (process.env.NODE_ENV === "production") {
//set a static folder
app.use(express.static("client/build"));
app.get("*", (req, res) => {
res.sendFile(path.resolve(__dirname, "client", "build", "index.html"));
});
}
const PORT = process.env.PORT || 5000;
app.listen(PORT, () => console.log(`Server is being ran on port ${PORT}`));
Where should I be looking to locate the issue?
After plenty of debugging, I have isolated the issue to Redux dev tools. The app worked with any browser that had Redux dev tools installed.
I figured out it had something to do with store.js file and examined my createStore:
const store = createStore(
rootReducer,
initialState,
compose(
applyMiddleware(...middleware),
window.__REDUX_DEVTOOLS_EXTENSION__ && window.__REDUX_DEVTOOLS_EXTENSION__()
)
);
After removing the following line from compose
window.REDUX_DEVTOOLS_EXTENSION &&
window.REDUX_DEVTOOLS_EXTENSION()
I pushed it to Heroku and the problem was solved.

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.

Port Timeout With NodeJS/Webpack Heroku Deployment

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.

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

Resources