Created
April 27, 2020 04:22
-
-
Save icandid/13d5439e8e40aaf05c311a0bc4980bc4 to your computer and use it in GitHub Desktop.
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
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