1

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?

1 Answer 1

4

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').

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.