This is my code for mongodb atlas connection, it shows connection successful but later when I click on some tab the app crashes. I m deploying app through heroku and i have given the error below .app runs fine on localhost but crashes on the heroku website. It sometimes allows and opens some pages but crashes right after that , i have tried fixing environemnt variables on heroku website but that was of no help so i switched to just using the mongo connection link through the variable. Any help is appreciated. I discovered that the server throws the error of not being able to connect after exactly 30 seconds but still cant find what is causign the error
if (process.env.NODE_ENV !== "production") {
require('dotenv').config();
}
const sanitize = require('express-mongo-sanitize');
// const dbURL = process.env.DB_URL;
const session = require('express-session');
const MongoStore = require('connect-mongodb-session')(session);
const dbURL = "mongodb+srv://RohanGupta:****************@*****.****.mongodb.net/oddevegame?retryWrites=true&w=majority";
mongoose.connect(dbURL, {
useNewUrlParser: true,
useCreateIndex: true,
useUnifiedTopology: true,
useFindAndModify: false
})
.then(() => {
console.log("Connected Mongo Successfully")
})
.catch(err => {
console.log("Uh Oh couldnt connect mongo!!")
console.log(err);
})
app.use(methodOverride('_method'));
app.use(express.urlencoded({ extended: true }));
app.use(express.static(path.join(__dirname, '/public')));
app.use(express.static(path.join(__dirname, '/resources')));
app.use(session({
store: new MongoStore({
url: dbURL,
touchAfter: 24 * 3600
}),
name: "blah",
secret: 'this is secret',
resave: false,
saveUninitialized: false,
cookie: {
httpOnly: true,
// secure: true,
expires: Date.now() + 1000 * 60 * 60 * 24 * 7,
maxAge: 1000 * 60 * 60 * 24 * 7
}
}));
2021-06-20T07:27:23.462279+00:00 heroku[web.1]: State changed from starting to up
2021-06-20T07:27:23.230556+00:00 app[web.1]: Listening on PORT 4000
2021-06-20T07:27:23.502833+00:00 app[web.1]: Connected Mongo Successfully
2021-06-20T07:27:53.221633+00:00 app[web.1]: /app/node_modules/mongodb/lib/utils.js:698
2021-06-20T07:27:53.221654+00:00 app[web.1]: throw error;
2021-06-20T07:27:53.221655+00:00 app[web.1]: ^
2021-06-20T07:27:53.221655+00:00 app[web.1]:
2021-06-20T07:27:53.221656+00:00 app[web.1]: Error: Error connecting to db: connect ECONNREFUSED 127.0.0.1:27017
2021-06-20T07:27:53.221656+00:00 app[web.1]: at /app/node_modules/connect-mongodb-session/index.js:78:17