Created
April 4, 2018 09:40
-
-
Save emilioriosvz/abdbe137737b830b21d66dc5f1236311 to your computer and use it in GitHub Desktop.
connect with mongoose using async/await
This file contains hidden or 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
const mongoose = require('mongoose') | |
mongoose.Promise = Promise | |
mongoose.connection.on('connected', () => { | |
console.log('Connection Established') | |
}) | |
mongoose.connection.on('reconnected', () => { | |
console.log('Connection Reestablished') | |
}) | |
mongoose.connection.on('disconnected', () => { | |
console.log('Connection Disconnected') | |
}) | |
mongoose.connection.on('close', () => { | |
console.log('Connection Closed') | |
}) | |
mongoose.connection.on('error', (error) => { | |
console.log('ERROR: ' + error) | |
}) | |
const run = async () => { | |
await mongoose.connect('mongodb://localhost:27017/emilioriosvz', { | |
autoReconnect: true, | |
reconnectTries: 1000000, | |
reconnectInterval: 3000 | |
}) | |
} | |
run().catch(error => console.error(error)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
(async () => await mongoose.connect(DB_URL))().catch(console.log);