We help IT Professionals succeed at work.

How can I connect to my MongoDB based on this Boilerplate?

High Priority
Last Modified: 2019-10-05
I've got a working app that I'm trying to assimilate into a Boilerplate. I'm thinking we're poised on the threshold of great things, but I'm stuck at the section where I've got to instantiate my Mongodb connection.

Here's the "index.js" code:

const mongoose = require('mongoose');
const util = require('util');

// config should be imported before importing any other file
const config = require('./config/config');
const app = require('./config/express');

const debug = require('debug')('express-mongoose-es6-rest-api:index');

// make bluebird default Promise
Promise = require('bluebird'); // eslint-disable-line no-global-assign

// plugin bluebird promise in mongoose
mongoose.Promise = Promise;

[b]// connect to mongo db
const mongoUri = config.mongo.host;
mongoose.connect(mongoUri, { server: { socketOptions: { keepAlive: 1 } } });
mongoose.connection.on('error', () => {
  throw new Error(`unable to connect to database: ${mongoUri}`);

// print mongoose logs in dev env
if (config.mongooseDebug) {
  mongoose.set('debug', (collectionName, method, query, doc) => {
    debug(`${collectionName}.${method}`, util.inspect(query, false, 20), doc);

// module.parent check is required to support mocha watch
// src: https://github.com/mochajs/mocha/issues/1912
if (!module.parent) {
  // listen on port config.port
  app.listen(config.port, () => {
    console.info(`server started on port ${config.port} (${config.env})`); // eslint-disable-line no-console

module.exports = app;

Open in new window

The part that I've got in bold is what's throwing the error.

Here's the config file:

const Joi = require('joi');

// require and configure dotenv, will load vars in .env in PROCESS.ENV

// define validation for all the env vars
const envVarsSchema = Joi.object({
  NODE_ENV: Joi.string()
    .allow(['development', 'production', 'test', 'provision'])
  PORT: Joi.number()
  MONGOOSE_DEBUG: Joi.boolean()
    .when('NODE_ENV', {
      is: Joi.string().equal('development'),
      then: Joi.boolean().default(true),
      otherwise: Joi.boolean().default(false)
  JWT_SECRET: Joi.string().required()
    .description('JWT Secret required to sign'),
  MONGO_HOST: Joi.string().required()
    .description('Mongo DB host url'),
  MONGO_PORT: Joi.number()

const { error, value: envVars } = Joi.validate(process.env, envVarsSchema);
if (error) {
  throw new Error(`Config validation error: ${error.message}`);

const config = {
  env: envVars.NODE_ENV,
  port: envVars.PORT,
  mongooseDebug: envVars.MONGOOSE_DEBUG,
  jwtSecret: envVars.JWT_SECRET,
  mongo: {
    host: envVars.MONGO_HOST,
    port: envVars.MONGO_PORT

module.exports = config;

Open in new window

...and here's the .env file that's been modified in the name of discretion:

JWT_SECRET=something really secure

Open in new window

The part that's in bold is a carry over from a tutorial that I worked through. This particular boilerplate has this instead for the MONGO_HOST:


Open in new window

Now, when I try to run things using my MONGO_HOST value, I get this error:

Error: Invalid mongodb uri. Must begin with "mongodb://"

Not sure how that would look based on the way I've learned it initially.

What do you think?

I'm new to the MERN stack. The syntax that I used to connect to my Mongodb has my credentials embedded in the URL. Not sure how this particular dynamic is supposed to work...
Watch Question

Most Valuable Expert 2017
Distinguished Expert 2019
What do you think?
I think you should listen to the error and use the BolierPlate convention of prefixing your URL with mongodb://