PowerLove
PowerLove

Reputation: 303

Using redis client in express router

I'd like to use redis client at certain endpoints.

my app.js is set up like this

//app.js

const redis = require('redis');
const client = redis.createClient(process.env.REDIS_PORT, 'redis');
const api = require('./routes/api');
const app = express();
const passport = require('passport');
app.use('/api', api(passport));

//api.js

module.exports = function (passport) {
    router.get("/reset", reset.resetPassword)
    return router
};

//reset.js

module.exports = (function () {

var resetPassword = function(req, res) {
    //do something with redis client here
}

return {
    resetPassword: resetPassword
}
})()

How can I pass the redis client to the resetPassword function? I tried passing it to the api.js then reset.js but resetPassword function doesn't seem to like parameters other than req, res and next...

Upvotes: 1

Views: 2031

Answers (1)

Tsvetan Ganev
Tsvetan Ganev

Reputation: 8856

You can export the Redis client as a module and use it in your route handlers.

// redis-client.js
const redis = require('redis');
const client = redis.createClient(process.env.REDIS_PORT, 'redis');
module.exports = client;

// reset.js
const redisClient = require('./redis-client');
var resetPassword = function(req, res) {
 // do something with redis client here
}

Upvotes: 6

Related Questions