I have worked to node js + mysql. but now i am going to node js + mongoDB I am in need to connect database and make a variable for globally use. I am very much confused how we can define variable for globally use.
var mongodb = require('mongodb');
var MongoClient = mongodb.MongoClient;
var url = 'mongodb://localhost:27017/mydb';
MongoClient.connect(url, function (err, db) {
if (err) {
console.log('Unable to connect to the mongoDB server. Error:', err);
} else {
//HURRAY!! We are connected. :)
console.log('Connection established to', url);
// do some work here with the database.
db.close();
}
});