Nodejs - Re-use MongoDB database connection in routes

Quite often when you are writing an application you will need access to one or more database connections. Maybe MongoDB for data storage and Redis for cache. You will need to re-use that database connection throughout your application. I'm going to go through a simple way of re-using the connection in modules, Express routes etc.

Creating the helper

Firstly you will want to create your db.js file which will export some handy database related functions.

File: db.js

const mongoClient = require('mongodb').MongoClient;
const mongoDbUrl = 'mongodb://';
let mongodb;

function connect(callback){
    mongoClient.connect(mongoDbUrl, (err, db) => {
        mongodb = db;
function get(){
    return mongodb;

function close(){

module.exports = {

After creating this file you can simply require it and you now have few functions at our disposal. connect, get, close.


File: app.js

You will then want to call connect() before your application starts and the server starts listening. Eg:

db.connect(() => {
    app.listen(process.env.PORT || 5555, function (){

Now you have access to your database connection anywhere in your application by simply requiring the db.js file and using the get() function.

Using the connection

File: users.js (routes file for example)

const db = require('./db');

router.get('/users', (req, res) => {
	.then((users) => {
            console.log('Users', users);

It just makes everything much cleaner and easy to handle this way. I hope this helped you in some way.