I have a default Express app, and my routes index.js file looks like this:
var user = require('../models/user');
exports.index = function(req, res){
res.render('index', { title: 'Express', object: obj });
};
Now, suppose in my app.js file the client
object that is the redis client connected to my redis-server, and I would like to be able to use that client object in my user model, for example to avoid repeated login attempts. How can I be able to access that object in my user model? Here is the code for that model
var mongoose = require('mongoose');
var userSchema = mongoose.Schema({
email: { type: String, required: true, index: { unique: true } },
password: { type: String, required: true },
name: { type: String, required: true }
});
var User = mongoose.model('User', userSchema);
exports.canLogin = function(mIn, pIn, next){
User.findOne({
email: mIn
}, function(err, user){
if(!err){
if(user.password == pIn)
next(true);
else next(false);
} else next(false);
});
};
If you want to store your client in the session, you can use connect-redis to setup session storage in express like so
Now, you can access the session like this
req.session.WHATEVER
(And you can do the same thing with mongo.)
If you just want some redis storage available when processing the request, you can use node_redis directly. There are plenty of examples on github