Proper way to re-user mongodb connection in node.js

I tried searching on the web and found no obvious code samples of how reuse the mongodb.connection object. This is what I have currently and would anyone please verify if this is okay.

var app = express();
var mongodb = require('mongodb').MongoClient, format = require('util').format;
var db = null;

app.get('/api/v1/put/:var1/:var2', function(req, res){
  collection = db.collection('collection');
  /** .. logic ... **/
});

mongodb.connect('mongodb://127.0.0.1:27017/mydb', function(err, mdb){
    db = mdb;
    app.listen(8000);
});

Your approach will have problem that once application runs it will register express route. If there is idling connections to your web server, then they will be processed ASAP, that will lead to db is undefined.

In order to prevent this, I would recommend to register express routing only after database is connected.
As well you can cache collections instead of getting them on each request.

this is just to refactor my question's code to reflect Maksims' suggestion

var app = express();
var mongodb = require('mongodb').MongoClient, format = require('util').format;

mongodb.connect('mongodb://127.0.0.1:27017/mydb', function(err, db){
    collection = db.collection('collection');

    app.get('/api/v1/put/:var1/:var2', function(req, res){
      /** .. logic ... **/
    });

    app.listen(8000);
});