Bruce Gust
asked on
How can I connect to my MongoDB based on this Boilerplate?
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:
The part that I've got in bold is what's throwing the error.
Here's the config file:
...and here's the .env file that's been modified in the name of discretion:
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:
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...
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}`);
});[/b]
// 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;
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
require('dotenv').config();
// define validation for all the env vars
const envVarsSchema = Joi.object({
NODE_ENV: Joi.string()
.allow(['development', 'production', 'test', 'provision'])
.default('development'),
PORT: Joi.number()
.default(4040),
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()
.default(27017)
}).unknown()
.required();
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;
...and here's the .env file that's been modified in the name of discretion:
NODE_ENV=development
PORT=4040
JWT_SECRET=something really secure
[b]MONGO_HOST=mongodb+srv://georgejones:creativepassword@cinderella-qhxnz.mongodb.net/smokinaccount[/b]
MONGO_PORT=27017
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:
NODE_ENV=development
PORT=4040
JWT_SECRET=somethingreallysecure
MONGO_HOST=mongodb://localhost/express-mongoose-es6-rest-api-development
MONGO_PORT=27017
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...
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.