-
-
Save mflisikowski/9f783e36ef34e6fad3a3e750125f22a3 to your computer and use it in GitHub Desktop.
Configuration I have been using for connecting to MongoDB Atlas
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ENVIRONMENT=development | |
HOSTNAME=localhost:4000 | |
MONGODB_DB=reactnodenew | |
MONGODB_HOST=localhost | |
MONGODB_PASS= | |
MONGODB_PORT=27017 | |
MONGODB_QUERIES= | |
MONGODB_SSL=FALSE | |
MONGODB_USER= | |
NODE_ENV=development |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require('dotenv').config({ path: './.localenv' }) | |
var config = {} | |
config.hostname = process.env.HOSTNAME || 'localhost:3000' | |
config.mongodb = {} | |
config.mongodb.host = process.env.MONGODB_HOST || '127.0.0.1' | |
config.mongodb.port = process.env.MONGODB_PORT || 27017 | |
config.mongodb.user = process.env.MONGODB_USER || '' | |
config.mongodb.password = process.env.MONGODB_PASS || '' | |
config.mongodb.database = 'react' | |
config.mongodb.queries = process.env.MONGODB_QUERIES || '' | |
//define the mongo address | |
config.mongodb.uri = 'mongodb://' | |
if (config.mongodb.user.length && config.mongodb.password.length) | |
config.mongodb.uri += | |
config.mongodb.user + ':' + config.mongodb.password + '@' | |
config.mongodb.uri += config.mongodb.host | |
if (config.mongodb.port.toString().length) | |
config.mongodb.uri += ':' + config.mongodb.port.toString() | |
if (config.mongodb.database.length) | |
config.mongodb.uri += '/' + config.mongodb.database | |
if (process.env.MONGODB_SSL == 'TRUE') { | |
config.mongodb.ssl = true | |
config.mongodb.uri += '?ssl=true' | |
} else { | |
config.mongodb.ssl = false | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//inside models directory (models/db.js | |
'use strict'; | |
var config = require('../config'); | |
var mongoose = require('mongoose'); | |
mongoose.Promise = global.Promise; | |
var connection; | |
var connect = function() { | |
console.log("connecting over SSL? ", config.mongodb.ssl) | |
var options = { | |
server: { | |
ssl: config.mongodb.ssl, | |
auto_reconnect: true, | |
replset: { | |
socketOptions: { | |
connectTimeoutMS: 120000, | |
socketTimeoutMS: 120000, | |
keepAlive: 1 | |
} | |
}, | |
socketOptions: { | |
connectTimeoutMS: 120000, | |
socketTimeoutMS: 120000, | |
keepAlive: 1 | |
} | |
} | |
} | |
if (config.mongodb.queries != "") { | |
config.mongodb.queries = "&" + config.mongodb.queries | |
} | |
mongoose.connect(config.mongodb.uri + config.mongodb.queries, options); | |
console.log("FINAL: " + config.mongodb.uri + config.mongodb.queries) | |
}; | |
connect(); | |
mongoose.connection.on('error', console.log); | |
mongoose.connection.on('disconnected', connect); | |
module.exports.getConnection = function() { | |
return mongoose.connection; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment