Skip to content

Commit

Permalink
added mongo db config and handled uncaught error
Browse files Browse the repository at this point in the history
  • Loading branch information
Prasun Jaiswal committed Mar 1, 2020
1 parent 150d435 commit b1b02bf
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
10 changes: 10 additions & 0 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,20 @@ process.on('SIGINT', () => {
process.exit(1);
});

// uncaught promise rejection
process.on('unhandledRejection', (reason, p) =>
error('Unhandled Rejection at: Promise ', p, reason)
);

// uncaught exception
process.on('uncaughtException', (err, origin) => {
error(
process.stderr.fd,
`Caught exception: ${err}\n` +
`Exception origin: ${origin}`);
});


let server;

// checking the environment of the node
Expand Down
34 changes: 34 additions & 0 deletions src/config/db.mongo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
const mongoose = require('mongoose');
const chalk = require('chalk');
const {
log,
error,
info
} = require('../utils/logging');

function connectDb() {
mongoose.connect(process.env.MONGO_URI, {
useNewUrlParser: true,
useUnifiedTopology: true
});

mongoose.connection.on('connected', function () {
info(chalk.blue(' [ ✓ ] ') + 'Application - Connected to MongoDb');
});

mongoose.connection.on('error', function (err) {
error('Mongoose default connection has occured ' + err + ' error')
});

mongoose.connection.on('disconnected', function () {
info('Mongoose default connection is disconnected')
});

process.on('SIGINT', function () {
mongoose.connection.close(function () {
info('Mongoose default connection is disconnected due to application termination')
});
});
}

module.exports = connectDb()

0 comments on commit b1b02bf

Please sign in to comment.