node.js app getting the R10 error on Heroku - node.js

I have previously looked into similar questions but non of the solutions worked out for me which is why I am asking this question. I am simply trying to deploy my node.js application to heroku but I keep getting an application error.
the following is a part of my logs:
2018-09-29T18:54:45.545079+00:00 app[web.1]: > pg_backend#1.0.0 start /app
2018-09-29T18:54:45.545081+00:00 app[web.1]: > node server.js
2018-09-29T18:54:45.545082+00:00 app[web.1]:
2018-09-29T18:54:46.035221+00:00 app[web.1]: listening to requests...
2018-09-29T18:55:43.134627+00:00 heroku[web.1]: Error R10 (Boot timeout) -> Web process failed to bind to $PORT within 60 seconds of launch
2018-09-29T18:55:43.134859+00:00 heroku[web.1]: Stopping process with SIGKILL
2018-09-29T18:55:43.317846+00:00 heroku[web.1]: Process exited with status 137
2018-09-29T18:55:43.329780+00:00 heroku[web.1]: State changed from starting to crashed
2018-09-29T21:23:23.635112+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=project-gorilla-backend.herokuapp.com request_id=2ec25806-0c83-4a7d-9bd8-eab41a67e996 fwd="131.227.39.211" dyno= connect= service= status=503 bytes= protocol=https
2018-09-29T21:23:24.637219+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" host=project-gorilla-backend.herokuapp.com request_id=a0582179-52fe-4c55-ba87-b0c146d66962 fwd="131.227.39.211" dyno= connect= service= status=503 bytes= protocol=https
The following is my package.json file:
{
"name": "pg_backend",
"version": "1.0.0",
"description": "back end application for the project gorilla site",
"main": "index.js",
"scripts": {
"start": "node server.js"
},
"engines": {
"node": "8.10.0",
"npm": "5.6.0"
},
"author": "Christopher Salay",
"license": "ISC",
"dependencies": {
"express": "^4.16.3",
"nodemailer": "^4.6.8",
"socket.io": "^2.1.1",
"vimeo": "^2.1.0"
}
}
and this is my Procfile:
web: node server.js
the following is my server.js
let express = require('express'), app = express(), path = require('path'),
socket = require('socket.io'), emailModule = require('./email.js'),
formValidationModule = require('./formValidation.js'), vimeoModule = require('./vimeo.js'),
port = process.env.PORT || 5000;
app.get('/', function(req, res) {
res.sendFile(path.join(__dirname + '/index.html'));
});
let server = app.listen(port,'0.0.0.0',function(){
console.log('listening to requests...');
vimeoModule.search()
});
let io = socket(server);
io.on('connection', (socket)=>{
console.log('made a connection!');
socket.on('email', (data)=>{
if(formValidationModule.checkEmptyContact(data.client, data.email, data.name,
data.title, data.message)){
socket.emit('invalidData');
}
else {
/**
* * Here you need to set your email options which includes the clients email, destination email,
* subject and the text (the email content).
*/
emailModule.setMailOptions(data.email,/*'Info#project-gorilla.co.uk'*/'salay777#hotmail.co.uk'
, data.client + ' ' + '(' +
data.name + ')' + ' ' + data.title, data.message).then((mailOpts)=>{
emailModule.send(mailOpts)
});
console.log('email has been sent!');
}
});
});
link to the heroku app:
https://pg-gorilla-backend.herokuapp.com/
and I use the following command to deploy to heroku:
git push heroku master

You need to configure your port on heroku as you cannot pass static port to the app when deploying to heroku, Heroku has built in method to bind designated port to app which is deployed on its environment. You need to use env var to assign port to the app.
In heroku app you can do this by going through Settings > Show config vars > Add PORT 3000 in it and then heroku in built method will assign designated PORT number to the app.

Related

Deploying Node.js app to Heroku with error H10 stating that "/tmp/start-d846bb8e.sh: 1: nodemon: not found"

So I've been trying to deploy my app with Heroku, and it runs locally perfectly fine, but I keep running into the same H10 error below:
2022-09-15T02:57:14.484472+00:00 heroku[web.1]: Starting process with command `npm start`
2022-09-15T02:57:17.725863+00:00 app[web.1]:
2022-09-15T02:57:17.725906+00:00 app[web.1]: > node-sso-example-app#1.0.0 start
2022-09-15T02:57:17.725912+00:00 app[web.1]: > nodemon index.js
2022-09-15T02:57:17.725915+00:00 app[web.1]:
2022-09-15T02:57:17.731558+00:00 app[web.1]: /tmp/start-d846bb8e.sh: 1: nodemon: not found
2022-09-15T02:57:17.847830+00:00 heroku[web.1]: Process exited with status 127
2022-09-15T02:57:17.916430+00:00 heroku[web.1]: State changed from starting to crashed
2022-09-15T02:57:19.230286+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=will-dersh.herokuapp.com request_id=9e07b48e-dcd7-42eb-b6bc-d9d7e138b15a fwd="108.67.29.121" dyno= connect= service= status=503 bytes= protocol=https
2022-09-15T02:57:19.499659+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" host=will-dersh.herokuapp.com request_id=f7501cb9-60c7-457d-97ed-63cffdfe872f fwd="108.67.29.121" dyno= connect= service= status=503 bytes= protocol=https
Here's my index.js file:
import express from 'express'
import 'dotenv/config'
import router from './routes/index.js'
import morgan from 'morgan'
const app = express()
app.use('/public', express.static('public'))
app.use(express.urlencoded({ extended: false }))
app.use(express.json())
app.use(morgan('dev'))
app.use('/', router)
app.listen(process.env.PORT || 8000, function(){
console.log("Express server listening on port %d in %s mode", this.address().port, app.settings.env);
});
Here's the package.json:
{
"name": "node-sso-example-app",
"version": "1.0.0",
"description": "Example Node.js SSO App using WorkOS",
"main": "index.js",
"type": "module",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "nodemon index.js"
},
"author": "WorkOS",
"dependencies": {
"#workos-inc/node": "^2.11.0",
"cookie-parser": "^1.4.6",
"dotenv": "^16.0.1",
"ejs": "^3.1.8",
"express": "^4.18.1",
"express-session": "^1.17.2",
"http-errors": "~1.6.3",
"morgan": "^1.10.0",
"router": "^1.3.7"
},
"devDependencies": {
"nodemon": "^2.0.19"
}
}
And if it helps, here is my Procfile:
web:node index.js
I'm not really sure where I've gone wrong so any help would be appreciated!

Heroku shows an application error when I try to get another route

Please help
I'm tired of trying to do it but I can't !
After deploying to Hiruku, the home page loads, but whenever I click on another route, it shows this
Application error
Then when I restart Heroku, the error goes away, but when I hit another route, it shows Application error again and crashes the app.
Thanks in advance.
Error in Terminal
(/app/node_modules/mongodb/lib/operations/connect.js:29:9)
2022-08-19T09:21:41.269285+00:00 app[web.1]: at /app/node_modules/mongodb/lib/operations/connect.js:79:20
2022-08-19T09:21:41.269285+00:00 app[web.1]: at exitWithError (/app/node_modules/mongodb/lib/sdam/topology.js:187:53)
2022-08-19T09:21:41.269286+00:00 app[web.1]: at /app/node_modules/mongodb/lib/sdam/topology.js:191:59
2022-08-19T09:21:41.269286+00:00 app[web.1]: at /app/node_modules/mongodb/lib/sdam/topology.js:244:80
2022-08-19T09:21:41.269286+00:00 app[web.1]: at <anonymous>
2022-08-19T09:21:41.396792+00:00 heroku[web.1]: Process exited with status 1
2022-08-19T09:21:41.443509+00:00 heroku[web.1]: State changed from up to crashed
2022-08-19T09:23:05.484764+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/golpo" host=gontobyo.herokuapp.com request_id=5a8b7e57-efc9-4b9d-a069-f80cb2bf0000 fwd="27.147.190.218" dyno= connect= service= status=503 bytes= protocol=https
2022-08-19T09:23:05.709953+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" host=gontobyo.herokuapp.com request_id=34668197-9b3c-4bfc-af99-d287ef6c9e11 fwd="27.147.190.218" dyno= connect= service= status=503 bytes= protocol=https
2022-08-19T09:46:27.638802+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=gontobyo.herokuapp.com request_id=0956306d-eb94-49c3-ad63-843ac7d40c01 fwd="27.147.190.218" dyno= connect= service= status=503 bytes= protocol=https
2022-08-19T09:46:27.862759+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" host=gontobyo.herokuapp.com request_id=867140a3-8b28-414c-9505-905fc68692e3 fwd="27.147.190.218" dyno= connect= service= status=503 bytes= protocol=https
package.json {
"dependencies": {
"bcrypt": "^5.0.1",
"connect-flash": "^0.1.1",
"connect-mongodb-session": "^3.1.1",
"cors": "^2.8.5",
"dotenv": "^16.0.1",
"ejs": "^3.1.8",
"express": "^4.18.1",
"express-session": "^1.17.3",
"express-validator": "^6.14.2",
"mongoose": "^6.5.2",
"multer": "^1.4.5-lts.1",
"nodemon": "^2.0.19"
},
"name": "project-6",
"version": "1.0.0",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node index.js",
"dev": "nodemon index.js"
},
"engines": {
"node": "16.16.0",
"npm": "8.11.0"
},
"keywords": [],
"author": "",
"license": "ISC",
"description": ""
}
index.js
// External Inputs
require("dotenv").config();
const mongoose = require("mongoose");
// Internal Inputs
const app = require("./app");
// DotEnv Inputs
const PORT = process.env.PORT || 5000;
// Mongoose Connection
mongoose
.connect(process.env.DB)
.then(() => {
console.log(`DB is connected`);
})
.catch((error) => {
console.log(`DB is not connected! ${error}`);
process.exit(1);
});
// Node server
app.listen(PORT, () => {
console.log(`Server is running at http://localhost:${PORT}`);
});
app.js
// External Inputs
require("dotenv").config();
const express = require(`express`);
const cors = require(`cors`);
const app = express();
const session = require("express-session");
const MongoDBStore = require("connect-mongodb-session")(session);
const flash = require("connect-flash");
// Internal Inputs
const userRouter = require(`./routes/user.route`);
const {
bindUserWithRequest,
} = require(`./middleware/bindUserWithRequest.middleware`);
const setLocals = require(`./middleware/setLocals.middleware`);
//Mongo DB store
const store = new MongoDBStore({
uri: process.env.DB,
collection: "GontobyoSessions",
expires: 1000 * 60 * 60 * 2,
});
// Setting view engine
app.set("view engine", "ejs");
// Middlewares
app.use(cors());
app.use(express.static(`public`));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(
session({
secret: process.env.SECRET_KEY,
resave: false,
saveUninitialized: false,
store: store,
})
);
app.use(flash());
app.use(bindUserWithRequest());
app.use(setLocals());
app.use(userRouter);
app.use((req, res) => {
res.render("crush");
});
// Export App
module.exports = app;

Facing NodeJS deploying error on Heroku, how can I fix it?

I deployed my NodeJS app on Heroku, But I'm getting this error:
Image of error
I've tried to deploy my NodeJs app on Heroku, follows all steps on Heroku guide but I can't make it running.
How can I fix it?
This is my app.js :
const express = require('express');
const app = express();
const path = require('path');
app.get('/', async (req, res) => {
res.sendFile(path.join(__dirname + '/index.html'));
});
app.post('/hello', async (req, res) => {
// My code ....
});
app.listen(3000, () => console.log('listening on port 3000'));
This is my Package.json :
{
"name": "amer-api",
"version": "1.0.0",
"description": "Amer's personal website api.",
"author": "Amer Ansari",
"license": "ISC",
"homepage": "https://github.com/AmerAnsari/amer-api",
"repository": {
"type": "git",
"url": "git+https://github.com/AmerAnsari/amer-api.git"
},
"bugs": {
"url": "https://github.com/AmerAnsari/amer-api/issues"
},
"scripts": {
"start": "node app.js"
},
"main": "app.js",
"dependencies": {
"express": "^4.17.1",
"geoip-lite": "^1.4.2",
"nodemailer": "^6.4.6"
}
}
THere is the log (heroku logs --tail) :
amer#pire:~/Documents/GitHub/amer-api$ heroku logs --tail
2020-05-18T00:31:45.124394+00:00 app[api]: Initial release by user am3ransari#gmail.com
2020-05-18T00:31:45.124394+00:00 app[api]: Release v1 created by user am3ransari#gmail.com
2020-05-18T00:31:45.576958+00:00 app[api]: Enable Logplex by user am3ransari#gmail.com
2020-05-18T00:31:45.576958+00:00 app[api]: Release v2 created by user am3ransari#gmail.com
2020-05-18T00:32:22.000000+00:00 app[api]: Build started by user am3ransari#gmail.com
2020-05-18T00:32:49.104161+00:00 app[api]: Deploy 1d131d7d by user am3ransari#gmail.com
2020-05-18T00:32:49.104161+00:00 app[api]: Release v3 created by user am3ransari#gmail.com
2020-05-18T00:32:49.128088+00:00 app[api]: Scaled to web#1:Free by user am3ransari#gmail.com
2020-05-18T00:32:55.009626+00:00 app[web.1]:
2020-05-18T00:32:55.009659+00:00 app[web.1]: > amer-api#1.0.0 start /app
2020-05-18T00:32:55.009660+00:00 app[web.1]: > node app.js
2020-05-18T00:32:55.009660+00:00 app[web.1]:
2020-05-18T00:32:55.349511+00:00 app[web.1]: listening on port 3000
2020-05-18T00:32:58.000000+00:00 app[api]: Build succeeded
2020-05-18T00:33:53.578640+00:00 heroku[web.1]: State changed from starting to crashed
2020-05-18T00:33:53.583026+00:00 heroku[web.1]: State changed from crashed to starting
2020-05-18T00:34:01.570180+00:00 app[web.1]:
2020-05-18T00:34:01.570202+00:00 app[web.1]: > amer-api#1.0.0 start /app
2020-05-18T00:34:01.570202+00:00 app[web.1]: > node app.js
2020-05-18T00:34:01.570203+00:00 app[web.1]:
2020-05-18T00:34:02.125267+00:00 app[web.1]: listening on port 3000
2020-05-18T00:34:48.531089+00:00 heroku[router]: at=error code=H20 desc="App boot timeout" method=GET path="/" host=serene-shelf-12092.herokuapp.com request_id=82d5065f-fcd6-4e9c-8836-7ebb3a075658 fwd="92.99.250.143" dyno= connect= service= status=503 bytes= protocol=https
2020-05-18T00:34:59.080489+00:00 heroku[web.1]: State changed from starting to crashed
2020-05-18T00:35:00.097273+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=serene-shelf-12092.herokuapp.com request_id=aa2cac26-b857-48aa-a2eb-72f9c9abea04 fwd="92.99.250.143" dyno= connect= service= status=503 bytes= protocol=https
Anyone can help me?
please add path into your dependencies:
npm install path --save
secondly change your app.js last line to this
const port = process.env.PORT || 3000;
app.listen(port, () => console.log(`listening on:`, port));

Node.js app on Heroku returns "Process exited with status 127" in logs

I could not get my web page using node.js after deploying in heroku. I am getting the below error.
Application error
An error occurred in the application and your page could not be served. If you are the application owner, check your logs for details.
My log file content is below:
2017-07-29T12:59:20.918810+00:00 heroku[web.1]: Process exited with status 1
2017-07-29T13:00:02.740139+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=node-fgdp.herokuapp.com request_id=04e1aa35-0722-4947-a832-836884934abf fwd="223.30.48.118" dyno= connect= service= status=503 bytes= protocol=https
2017-07-29T13:00:04.519877+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" host=node-fgdp.herokuapp.com request_id=3ea243cd-db39-43eb-978a-c25c6682e8a0 fwd="223.30.48.118" dyno= connect= service= status=503 bytes= protocol=https
2017-07-29T13:00:25.971843+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=node-fgdp.herokuapp.com request_id=6df0d3e1-4ccf-442d-a7d4-46951fb2dfd1 fwd="223.30.48.118" dyno= connect= service= status=503 bytes= protocol=https
2017-07-29T13:00:26.328156+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" host=node-fgdp.herokuapp.com request_id=e69ce3b6-2b4f-4701-8313-19a5e9152669 fwd="223.30.48.118" dyno= connect= service= status=503 bytes= protocol=https
2017-07-29T13:06:18.001304+00:00 app[api]: Starting process with command `heroku run` by user subhrajyotipradhan#gmail.com
2017-07-29T13:06:19.926935+00:00 heroku[run.9255]: Awaiting client
2017-07-29T13:06:19.947866+00:00 heroku[run.9255]: Starting process with command `heroku run`
2017-07-29T13:06:20.133990+00:00 heroku[run.9255]: State changed from starting to up
2017-07-29T13:06:24.521994+00:00 heroku[run.9255]: Process exited with status 127
2017-07-29T13:06:24.533168+00:00 heroku[run.9255]: State changed from up to complete
I deployed my code into Heroku and when starts run the generated URL I am getting the above error. My code is below:
package.json:
{
"name": "FGDP",
"description": "Our sample Node to Heroku app",
"main": "server.js",
"scripts": {
"start": "node server.js"
},
"dependencies": {
"express": "~4.14.0",
"morgan": "~1.7.0",
"body-parser": "~1.15.2",
"method-override": "~2.3.7",
"mongojs": "~2.4.0",
"crypto-js": "~3.1.8",
"express-session": "~1.14.2",
"multer":"~1.3.0",
"dotenv" : "~4.0.0"
}
}
server.js:
var express=require('express');
var morgan = require('morgan');
var http=require('http');
var bodyParser= require('body-parser');
var methodOverride = require('method-override');
var mongo = require('mongojs');
var session = require('express-session');
var multer = require('multer')
var app=module.exports=express();
var server=http.Server(app);
const dotenv = require('dotenv');
dotenv.load();
var port=process.env.PORT;
var admin=require('./route/route.js');
app.use(express.static(__dirname + '/public')); // set the static files location /public/img will be /img for users
app.use(morgan('dev')); // log every request to the console
app.use(bodyParser.urlencoded({ extended: false })) // parse application/x-www-form-urlencoded
app.use(bodyParser.json()) // parse application/json
app.use(methodOverride()); // simulate DELETE and PUT
app.use(session({secret: 'FGDPlexel',resave: true,saveUninitialized: true}));
app.get('/',function(req,res){
res.sendFile(__dirname + '/index.html');
})
var storage =multer.diskStorage({
destination: function (req, file, callback) {
callback(null, './uploads');
},
filename: function (req, file, callback) {
callback(null, file.fieldname + '-' + Date.now());
}
});
app.post('/login',admin.userlogin);
app.get('/getSessionValue',admin.getSession);
server.listen(port);
console.log("Server is running on the port"+port);
I have set the port using dotenv and the file is given below.
.env:
PORT=8989;
Here I need to run the app.
Don't use dotenv. You can set environment variables from either the CLI or from the settings tab in the heroku dashboard. Read more about it here: https://devcenter.heroku.com/articles/config-vars#setting-up-config-vars-for-a-deployed-application
According to this issue dotenv doesn't work on Heroku in production environment. Use Heroku dashboard to set environment variables.

Why my heroku node.js app is giving at=error code=H10 desc="App crashed" method=GET path="/"?

I am trying to run my simple node app on Heroku.
Here is the directory structure
├── app.js
├── assets
├── blog.html
├── index.html
├── node_modules
└── package.json
Here is my app.js
let express = require('express'),
path = require('path');
var app = express();
let server = require('http').Server(app);
app.use(express.static(path.join(__dirname)));
app.get('/', function(req, res, next){
res.sendStatus(200);
});
app.get('/blog.html', function(req, res,next){
res.sendFile(path.join(__dirname+"/blog.html"));
});
app.post('/contact', function(req, res, next){
});
server.listen('8000', function() {
console.log("App is running on port 8000");
});
Here is the package.json
{
"name": "website",
"version": "1.0.0",
"engines" : {
"node" : "6.3.1",
"npm" : "3.10.3"
},
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start" : "node app.js"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"express": "^4.14.0"
}
}
When I go to the console it rightly prints app is starting at xxxx port.
But then the app crashes with the following message
2016-08-10T13:12:49.839138+00:00 app[web.1]: App is running on port xxxx
2016-08-10T13:13:34.944963+00:00 heroku[router]: at=error code=H20 desc="App boot timeout" method=GET path="/" host=saras-website.herokuapp.com request_id=28d8705a-d5a4-4aaa-bd8d-4c4c6101fbd4 fwd="106.51.20.181" dyno= connect= service= status=503 bytes=
2016-08-10T13:13:48.295315+00:00 heroku[web.1]: State changed from starting to crashed
2016-08-10T13:13:48.552740+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=saras-website.herokuapp.com request_id=b77e151f-7017-482d-b4ba-15d980534fd7 fwd="106.51.20.181" dyno= connect= service= status=503 bytes=
2016-08-10T13:13:50.163466+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" host=saras-website.herokuapp.com request_id=1e7b57e5-1056-4cb3-b41f-cd3f11794efe fwd="106.51.20.181" dyno= connect= service= status=503 bytes=
I don't know what am I doing wrong here... Help is appreciated
Set port like this
ES5:
var port = process.env.PORT || 8000;
And
server.listen(port, function() {
console.log("App is running on port " + port);
});
ES6:
const port = process.env.PORT || 8000;
And
server.listen(port, () => {
console.log("App is running on port " + port);
});
This allows Heroku to set the port at run time.

Resources