implements db events

This commit is contained in:
Francesco Esposito 2019-02-22 10:12:24 +01:00
parent 25a149c4c9
commit 5424a064c6
1 changed files with 15 additions and 1 deletions

View File

@ -15,6 +15,8 @@ const connectionString = `${db.prefix}${db.user}:${db.password}@${db.host}/${db.
mongoose.connect(connectionString, { useNewUrlParser: true });
mongoose.set('useFindAndModify', false);
const mongodb = mongoose.connection;
app.use((err, req, res, next) => {
logger.logError(err);
res.status(500);
@ -33,12 +35,24 @@ app.listen(port, () => {
logger.logConsole('Server started.');
});
mongodb.on('connected', () => {
logger.logInfo('DB connected');
});
mongodb.on('disconnected', () => {
logger.logInfo('DB disconnected');
});
mongodb.on('error', (err) => {
logger.logError(`DB error:' ${err}`);
});
process.on('uncaughtException', (err) => {
logger.logError(err);
});
process.on('SIGINT', () => {
mongoose.connection.close();
mongoose.disconnect();
logger.logInfo('Server stopped');
logger.logConsole('Server stopped');
process.exit(0);