I'm creating a express 4 application. I have a redis helper class in my models folder. Now I want to be able to use the same instance across all files, specifically within my different routes files. What is the best way to do this?
//models/redis.js
var redis = require("redis");
var client = redis.createClient();
...
function redisObject(){
this.redis = redis;
this.client = client;
}
module.exports = new redisObject();
I then create an instance of this object in my app.js file
//app.js
var db = require('./models/redis');
Where do I store db so that I have access to it globally? Am I going down the wrong path of thinking here?
Why not just use require('./models/redis')
within the other files too? They will all get the same object because exports
is cached during the first require('./models/redis')
.