Last active
August 13, 2019 13:31
-
-
Save chrisckchang/6907710 to your computer and use it in GitHub Desktop.
mongoose connection options
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
/* Mongoose 3.8.x configuration settings | |
* | |
* As discussed in: www.blogpost.com | |
*/ | |
var mongoose = require('mongoose'); | |
var uriUtil = require('mongodb-uri'); | |
/* | |
* Mongoose by default sets the auto_reconnect option to true. | |
* We recommend setting socket options at both the server and replica set level. | |
* We recommend a 30 second connection timeout because it allows for | |
* plenty of time in most operating environments. | |
*/ | |
var options = { server: { socketOptions: { keepAlive: 1, connectTimeoutMS: 30000 } }, | |
replset: { socketOptions: { keepAlive: 1, connectTimeoutMS : 30000 } } }; | |
/* | |
* Mongoose uses a different connection string format than MongoDB's standard. | |
* Use the mongodb-uri library to help you convert from the standard format to | |
* Mongoose's format. | |
*/ | |
var mongodbUri = 'mongodb://user:pass@host:port/db'; | |
var mongooseUri = uriUtil.formatMongoose(mongodbUri); | |
mongoose.connect(mongooseUri, options); | |
var conn = mongoose.connection; | |
conn.on('error', console.error.bind(console, 'connection error:')); | |
conn.once('open', function(err) { | |
if(err) throw err; | |
// Wait for the database connection to establish, then start the app. | |
app.listen(3000); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment