Skip to content

Instantly share code, notes, and snippets.

@icandid
Created April 27, 2020 04:22
Show Gist options
  • Save icandid/13d5439e8e40aaf05c311a0bc4980bc4 to your computer and use it in GitHub Desktop.
Save icandid/13d5439e8e40aaf05c311a0bc4980bc4 to your computer and use it in GitHub Desktop.
const mongoose = require('mongoose')
const dbURI = 'mongodb://localhost/Loc8r'
mongoose.connect(dbURI, {
useNewUrlParser: true,
useUnifiedTopology: true
})
mongoose.connection.on('connected', () => {
console.log(`Mongoose connected to ${dbURI}`)
})
mongoose.connection.on('error', err => {
console.log(`Mongoose connection error:`, err)
})
mongoose.connection.on('disconnected', () => {
console.log('mongoose disconnected')
})
const gracefulShutdown = (msg, cb) => {
mongoose.connection.close(() => {
console.log(`Mongoose disconnected through ${msg}`)
cb()
})
}
process.once('SIGUSR2', () => {
gracefulShutdown('nodemon restart', () => {
process.kill(process.pid, 'SIGUSR2')
})
})
process.on('SIGINT', () => {
gracefulShutdown('app termination', () => {
process.exit(0)
})
})
process.on('SIGTERM', () => {
gracefulShutdown('Heroku app shutdown', () => {
process.exit(0)
})
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment