Help with Heroku NodeJs deployment error

I’m very new to deploying apps/development in general. Any help with this would be much appreciated, I’ve been stuck for days.

I have no issues deploying node apps to Heroku, unless there is a database involved (I use mongoDB). From my research, I thought maybe I needed to use MLAB so I signed up for that and created a DB, hooked it up to my app etc. but still no luck with deployment. When I run the app on localhost everything works fine, I can query the database etc. However once deployed to Heroku I get Application Error (the app queries the DB on load of homepage).

Here is my package.json:

{
  "name": "blog",
  "version": "1.0.0",
  "description": "My personal blog",
  "main": "app.js",
  "scripts": {
    "start": "node app.js",
    "dev": "nodemon app.js"
  },
  "author": "Jack Fisher",
  "license": "ISC",
  "dependencies": {
    "bcryptjs": "^2.4.3",
    "connect-flash": "^0.1.1",
    "dotenv": "^7.0.0",
    "ejs": "^2.6.1",
    "express": "^4.16.4",
    "express-ejs-layouts": "^2.5.0",
    "express-session": "^1.15.6",
    "jquery": "^3.3.1",
    "mongodb": "^3.2.3",
    "mongoose": "^5.5.0",
    "passport": "^0.4.0",
    "passport-local": "^1.0.0"
  },
  "devDependencies": {
    "nodemon": "^1.18.11"
  },
  "engines": {
    "node": "10.14.2"
  }
}

Here is my app.js:

const express = require('express');

const expressLayouts = require('express-ejs-layouts');

const mongoose = require('mongoose');

const flash = require('connect-flash');

const session = require('express-session');

const passport = require('passport');

const app = new express();

require('dotenv').config();

//EJS templating - the master template is layout.ejs

app.use(expressLayouts);

app.set('view engine', 'ejs');

//Passport config

require('./config/passport')(passport);

// DB Config

const db = require('./config/keys').MongoURI;

// possible uri that heroku made - might need to store in variable process.env.MONGODB_URI

//const db = require('./config/keys').MONGODB_URI;

//connect to Mongo

mongoose.connect(db, { useNewUrlParser: true })

.then(() => console.log("mongoDB connected..."))

.catch(err => console.log(err));

//Bodyparser

app.use(express.urlencoded({ extended: false }));

// Express session

app.use(session({

secret: 'secret',

resave: true,

saveUninitialized: true,

}))

// Passport middleware

app.use(passport.initialize());

app.use(passport.session());

//Connect flash - flash allows you to display success and error messages

app.use(flash());

//Global Variables - set error messages to variables here

app.use((req, res, next) => {

res.locals.success_msg = req.flash('success_msg');

res.locals.error_msg = req.flash("error_msg");

res.locals.error = req.flash("error");

next();

});

app.use(express.static('public'));

// External routes

app.use('/', require('./routes/index'));

app.use("/users", require("./routes/users"));

const PORT = process.env.PORT || 7500;

app.listen(PORT, () => console.log(`Server started on port ${PORT}`));

Here is my mongo db stuff:

require('dotenv').config();

const dbpass = process.env.DB_PASS;
const mlabpass = process.env.MLAB_PASS;

module.exports = {
  MongoURI:
    `mongodb://fishj123:${mlabpass}@ds149885.mlab.com:49885/heroku_26921dvn`,
    
};

For all the code, see my git repo - github [dot] com/ fishj123 /personal-blog

I’ve checked the error logs in Heroku and one line that stands out is:

2019-04-14T12:51:38.326151+00:00 app[web.1]: { MongoError: Authentication failed.

This suggests my username/password are incorrect for connecting to the db but it lets me read/write to the db on localhost using the same credentials so does that not mean that the credentials are correct?

Thanks in advance to anyone that can help me out.

Go to settings page of your app and click on config variables.

Store your keys in there with keynames matching process.env...

1 Like

Thank you so much. I had no idea I needed to do that in Heroku and I feel stupid now.