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.
Related
I have completed a project and trying to deploy on heroku. I am using Reactjs frontend and express Nodejs with mongoose and mongodb in the Backend. It works on localhost but when I run Build it and try to deploy it on heroku it gives an application error.
Here is my backend connection code:
require('dotenv').config();
const express = require('express')
const session = require("express-session")
const bodyParser = require('body-parser')
const cors = require('cors')
const zomatoRoutes = require('./Routes/zomato')
const paymentRoutes = require('./Routes/payments')
const mongoose = require('mongoose')
const passport = require("passport")
const MongoStore = require('connect-mongo');
const uri = process.env.MONGO_URI || 'mongodb://localhost/zomato';
console.log(uri,'this is the mongo Atlas uri if connected using that !!!')
const options = {
useNewUrlParser: true,
useUnifiedTopology: true,
dbName:'zomato1'
}
const connectDB = async () => {
await mongoose.connect(uri, options ).then(() => {
console.log('mongo connected')
}).catch( e => console.log(e))
}
connectDB().then(() => {
app.listen( process.env.PORT ||5252 , () => {
console.log("express app is up and running on port 5252");
})
})
I also have mongo session store the mongoose session when user logs in
app.use(session({
secret: "this is unambigous secret",
resave: false,
saveUninitialized: false,
cookie: { maxAge: 24*60*60*1000 },
store : MongoStore.create({
client: mongoose.connection.getClient(),
dbName:'zomato1',
ttl: 14 * 24 * 60 * 60
})
}));
app.use(passport.initialize());
app.use(passport.session());
It gives me MongoNotConnectedError: MongoClient must be connected to perform this operation error when I try to deploy using
git push heroku master:main
full error tail :
mongodb+srv://vishal_torne_22:********#db-first-hernode.zu6btrs.mongodb.net/<DBNamecomeshere>?retryWrites=true&w=majority this is the mongo Atlas uri if connected using that !!!
2022-07-31T11:19:34.581869+00:00 app[web.1]: /app/node_modules/mongodb/lib/utils.js:367
2022-07-31T11:19:34.581876+00:00 app[web.1]: throw new error_1.MongoNotConnectedError('MongoClient must be connected to perform this operation');
2022-07-31T11:19:34.581877+00:00 app[web.1]: ^
2022-07-31T11:19:34.581877+00:00 app[web.1]:
2022-07-31T11:19:34.581878+00:00 app[web.1]: MongoNotConnectedError: MongoClient must be connected to perform this operation
2022-07-31T11:19:34.581878+00:00 app[web.1]: at getTopology (/app/node_modules/mongodb/lib/utils.js:367:11)
2022-07-31T11:19:34.581881+00:00 app[web.1]: at Collection.createIndex (/app/node_modules/mongodb/lib/collection.js:258:82)
2022-07-31T11:19:34.581881+00:00 app[web.1]: at MongoStore.setAutoRemove (/app/node_modules/connect-mongo/build/main/lib/MongoStore.js:147:35)
2022-07-31T11:19:34.581881+00:00 app[web.1]: at /app/node_modules/connect-mongo/build/main/lib/MongoStore.js:128:24
2022-07-31T11:19:34.581882+00:00 app[web.1]: at processTicksAndRejections (node:internal/process/task_queues:96:5)
2022-07-31T11:19:34.581882+00:00 app[web.1]:
2022-07-31T11:19:34.581882+00:00 app[web.1]: Node.js v17.9.1
2022-07-31T11:19:34.703486+00:00 heroku[web.1]: Process exited with status 1
2022-07-31T11:19:34.791551+00:00 heroku[web.1]: State changed from starting to crashed
I am using Mongoose to connect the Mongodb, It works on localhost without heroku but when I deploy it using heroku, It asks me to use MongoClient. Is it a requirement to use mongoClient on Heroku 20 stack, I also get warning to upgrade to new Heroku version, I tried to upgrade heroku using npm upgrade heroku to the specifed version it shows successful but again rolls back to heroku 20 stack.
here's what I tried but researching the previous answers :
I added on my mongodb atlas the whitelist 0.0.0.0/0 (includes you current IP address)
I tried to make the mongoose code async so that it connect firsts the database then app listens to the port.
changed the engine version so that it supports latest node and npm version on Heroku like:
{
"version": "1.0.0",
"engines": {
"node": "17.x",
"npm":"8.x"
}
}
Is it a requirement to use MongoClient on heroku 20 stack ? as the Error shows... and why it is showing error on Mongoose.connect()
Thanks in advance..!
As you say it runs fine in your local environment, the issue may be fixed by adding process.env.MONGO_URL to the config vars of your application, as the issue may originate when Heroku tries to connect to your MongoDB, but does not achieve this connection due to an invalid url that is stored in an env variable on your local device.
In locally the application works perfectly but when i deployed it on heroku it showing errors on heroku logs. Also data wasn't added in mongoDb database. Actually in the application i used google-oauth login so in locally it works perfectly but not in heroku. I've also whitelisted my ip on mongodb atlas but still can't figure out why this error getting.
Anybody help me plz.....
Here is my prod.js file
module.exports = {
googleClientID: process.env.GOOGLE_CLIENT_ID,
googleClientSecret: process.env.GOOGLE_CLIENT_SECRET,
mongoURI: process.env.MONGO_URI,
cookieKey: process.env.COOKIE_KEY
}
Here is my keys.js file
// Keys.js - figure out what credentials should return
if (process.env.NODE_ENV === "production") {
// So we are in prodution - return the prod set of keys
module.exports = require('./prod');
} else {
// We are developement - return the dev keys
module.exports = require('./dev');
}
Here is my index.js file
const express = require('express');
const mongoose = require('mongoose');
const passport = require('passport');
const cookieSession = require('cookie-session');
const keys = require('./config/keys');
require('./models/User');
require('./services/passport');
const app = express();
//Middleware
app.use(
cookieSession({
maxAge: 30 * 24 * 60 * 60 * 1000,
keys: [keys.cookieKey]
})
);
app.use(passport.initialize());
app.use(passport.session());
// mongoDb Connection
mongoose.connect(keys.mongoURI, {
useNewUrlParser: true
}).then(() => console.log('MongoDb Connect successfully'))
.catch(err => console.log(err));
// actual routes
require('./routes/authRoutes')(app);
const PORT = process.env.PORT || 5000;
app.listen(PORT, () => console.log(`Server is running in ${PORT}`));
Actually i getting two different errors first was this
2019-09-02T06:28:59.760121+00:00 app[web.1]: Warning: connect.session() MemoryStore is not
2019-09-02T06:28:59.760149+00:00 app[web.1]: designed for a production environment, as it will leak
2019-09-02T06:28:59.760151+00:00 app[web.1]: memory, and will not scale past a single process.
2019-09-02T06:28:59.763577+00:00 app[web.1]:
2019-09-02T06:28:59.763581+00:00 app[web.1]: /app/node_modules/mongoose/lib/connection.js:519
2019-09-02T06:28:59.763583+00:00 app[web.1]: throw new MongooseError('The `uri` parameter to `openUri()` must be a ' +
2019-09-02T06:28:59.763585+00:00 app[web.1]: ^
2019-09-02T06:28:59.763838+00:00 app[web.1]: MongooseError: The `uri` parameter to `openUri()` must be a string, got "undefined". Make sure the first parameter to `mongoose.connect()` or `mongoose.createConnection()` is a string.
2019-09-02T06:28:59.763842+00:00 app[web.1]: at new MongooseError (/app/node_modules/mongoose/lib/error/mongooseError.js:10:11)
2019-09-02T06:28:59.763844+00:00 app[web.1]: at NativeConnection.Connection.openUri (/app/node_modules/mongoose/lib/connection.js:519:11)
2019-09-02T06:28:59.763846+00:00 app[web.1]: at Mongoose.connect (/app/node_modules/mongoose/lib/index.js:321:15)
2019-09-02T06:28:59.763848+00:00 app[web.1]: at Object.<anonymous> (/app/index.js:24:10)
2019-09-02T06:28:59.763850+00:00 app[web.1]: at Module._compile (internal/modules/cjs/loader.js:701:30)
2019-09-02T06:28:59.763852+00:00 app[web.1]: at Object.Module._extensions..js (internal/modules/cjs/loader.js:712:10)
2019-09-02T06:28:59.763854+00:00 app[web.1]: at Module.load (internal/modules/cjs/loader.js:600:32)
And the second one is this
2019-09-02T08:49:22.115492+00:00 app[web.1]: { MongoNetworkError: failed to connect to server [productionproj-shard-00-00-qfouv.mongodb.net:27017] on first connect [MongoNetworkError: connection 5 to productionproj-shard-00-00-qfouv.mongodb.net:27017 closed]
So on...... as same as above
Here is my mongodb production url i've been added on heroku config vars
mongodb+srv://nansProdDeploy:nansProdDeploy#productionproj-qfouv.mongodb.net/test?retryWrites=true&w=majority
It's all about whitelisting the correct ip address. When we put our application on heroku we must whitelist the ip by clicking [ Allow Access From Anywhere ] button and then click ok your ip will be 0.0.0.0/0 this ip will allow to access your deployed heroku application. When you are using your application locally at that point of you have to choose [ Add Current Ip Address ] to access your application locally
I know my question is repeated. Please read the question completely before marking as duplicate or anything.
I am learning node.js to create rest api & doing great on it.Everything was going smooth till i found this error( EADDRINUSE) on restart the app. This issue eat my precious hours but still not able to find the correct fix.
If the application is running for the 1st time, it launch's smoothly with out any port change for some reason we stop or restart the app this error message while be thrown from the server. Many of them suggested just change the port everything work smooth and I do agree with them.But When server is live & used worldwide you can't change the port to restart.
Adding Snippet of my Code.
app.js
var express = require('express'); // call express
var app = express(); // define our app using express
var bodyParser = require('body-parser');
var config = require('./Config/config.js');
var api = require('./apis/api');
// configure app to use bodyParser()
// this will let us get the data from a POST
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
// ROUTES FOR OUR API
// =============================================================================
var router = express.Router(); // get an instance of the express Router
app.all('/api/write/'+config.WRITE_API_VERSION+'/:apiName', function (req, res) {
api.handle.apply(null, ['write', req.method.toLowerCase(), req.params.apiName, req, res]);
console.log("in api end1"+req.body);
});
app.all('/api/read/'+config.READ_API_VERSION+'/:apiName', function (req, res) {
api.handle.apply(null, ['read', req.method.toLowerCase(), req.params.apiName, req, res]);
console.log("in api end2");
});
app.use(function (req, res, next) {
res.contentType('application/json');
next();
});
// more routes for our API will happen here
// REGISTER OUR ROUTES -------------------------------
// all of our routes will be prefixed with /api
app.use('/api', router);
// START THE SERVER
// =============================================================================
app.listen(config.PORT);
console.log('Magic happens on port ' + config.PORT);
config.js
var configs = {
ENVIRONMENT: "development",
READ_API_VERSION: "v1.0",
WRITE_API_VERSION: "v1.0",
PORT: 8080
}
Object.freeze(configs);
module.exports = configs;
Package.json
"scripts": {
"start": "node app.js"
}
Error Message
node app.js
Magic happens on port 8080
events.js:72
throw er; // Unhandled 'error' event
^
Error: listen EADDRINUSE
at errnoException (net.js:901:11)
at Server._listen2 (net.js:1039:14)
at listen (net.js:1061:10)
at Server.listen (net.js:1135:5)
at EventEmitter.listen (/home/ubuntu/vcare_backend/VCare/node_modules/express/lib/application.js:617:24)
at Object.<anonymous> (/home/ubuntu/vcare_backend/VCare/app.js:47:5)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
After running the command sudo netstat -plnt
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:8080 0.0.0.0:* LISTEN 3277/node
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1073/sshd
tcp6 0 0 :::22 :::* LISTEN 1073/sshd
Your issue is that node is already running on port 8080. This is shown by the first line returned by the netstat/lsof command. Killing the process will allow you to restart the node process. You can do this on a mac by using the command
killall node
Where node is the name of the process.
Node is already running on your machine.
In Terminal, find the running Node process with
ps -ax | grep node
This will return a list of the running node processes. The first column in the resulting list of processes is the process id (pid).
Kill the running node process with
kill -9 <pid>
This will kill the node process and free up the port for you to run your server.
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()
$ 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.