title: set mongodb node.js connection once for all routes using mongoclient
body: <p>I'm new in node.js and mongo db and i have done my code like this in all my routes.</p>
  
  <pre><code>var express       = require('express');
  var router        = express.Router();
  var mongo         = require('mongodb');
  var MongoClient   = mongo.MongoClient;
  var ObjectID      = mongo.ObjectID;
  var collection;
  
  //Connection to mongo db using mongo client
  MongoClient.connect('mongodb://127.0.0.1:27017/mydb', function(err, db) {
  
      //connection error or success message
      if (err) {
          console.log('Unable to connect to the mongoDB server. Error:', err);
          throw err;
      } else {
          console.log("connected to the mongoDB");
      }
  
      //index
      router.get('/', function(req, res) {
          collection = db.collection('category');
          collection.find({}).toArray(function(err, category) {
              collection = db.collection('subcategory');
              collection.find({}).toArray(function(err, subcategory) {
                  collection = db.collection('product');
                  collection.find({}).toArray(function(err, product) {
                      collection = db.collection('banner');
                      collection.find({status: 'A'}).toArray(function(err, banner) {
                          console.log(banner);
                          res.render('home', 
                              {
                                  title           : 'Home', 
                                  categorys       : category,
                                  subcategorys    : subcategory,
                                  products        : product,
                                  banner          : banner 
                              }
                          );
                      });
                  });
              });
          });
      });
  });
  module.exports = router; 
  </code></pre>
  
  <p>please help me to make a connection in common and access it from all my routes without repeating the connection call. thanks in advance </p>