I am trying to integrate Redis sessions into my authentication system written in Node.js.
我正在尝试将Redis会话集成到用Node.js编写的身份验证系统中。
I have been able to successfully set up Redis server, connect-redis
and Express server.
我已经成功地设置了Redis服务器、connect-redis和Express服务器。
Here is my setup (just the important bit):
这是我的设置(只是重要的一点):
var express = require("express");
var RedisStore = require("connect-redis")(express);
var redis = require("redis").createClient();
app.use(express.cookieParser());
app.use(express.session({
secret: "thisismysecretkey",
store: new RedisStore({ host: 'localhost', port: 6379, client: redis })
}));
Now... How do I actually create, read and destroy the session? I have read tons of articles on how to setup connect-redis
and many questions here on SO, but I swear each one stops on just the configuration and does not explain how to actually use it...
现在…如何创建、读取和销毁会话?我已经阅读了大量关于如何设置connectredis的文章和这里的许多问题,但是我发誓每一个都只停留在配置上,没有解释如何实际使用它……
I am aware that that is probably extremely simple, but please don't downvote and just explain :).
我知道这可能非常简单,但请不要投反对票,只解释一下:)。
2 个解决方案
#1
21
That should be all there is to it. You access the session in your route handlers via req.session
. The sessions are created, saved, and destroyed automatically.
这就是一切。通过req.session访问路由处理程序中的会话。这些会话是自动创建、保存和销毁的。
If you need to manually create a new session for a user, call req.session.regenerate()
.
如果您需要手动为用户创建一个新的会话,请调用req.session. rebuild()。
If you need to save it manually, you can call req.session.save()
.
如果需要手动保存,可以调用req.session.save()。
If you need to destroy it manually, you can call req.session.destroy()
.
如果需要手动销毁,可以调用req.session.destroy()。
See the Connect documentation for the full list of methods and properties.
有关方法和属性的完整列表,请参阅连接文档。
#2
5
Consider this code.
考虑这段代码。
var express = require('express');
var redis = require("redis");
var session = require('express-session');
var redisStore = require('connect-redis')(session);
var bodyParser = require('body-parser');
var client = redis.createClient();
var app = express();
app.set('views', __dirname + '/views');
app.engine('html', require('ejs').renderFile);
app.use(session({
secret: 'ssshhhhh',
// create new redis store.
store: new redisStore({ host: 'localhost', port: 6379, client: client,ttl : 260}),
saveUninitialized: false,
resave: false
}));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: true}));
app.get('/',function(req,res){
// create new session object.
if(req.session.key) {
// if email key is sent redirect.
res.redirect('/admin');
} else {
// else go to home page.
res.render('index.html');
}
});
app.post('/login',function(req,res){
// when user login set the key to redis.
req.session.key=req.body.email;
res.end('done');
});
app.get('/logout',function(req,res){
req.session.destroy(function(err){
if(err){
console.log(err);
} else {
res.redirect('/');
}
});
});
app.listen(3000,function(){
console.log("App Started on PORT 3000");
});
So you need to install connect-redis and pass your express-session instance to it.
因此,您需要安装connectredis并将您的express-session实例传递给它。
Then in middleware initialize redisStore with server details like this.
然后在中间件中使用服务器细节初始化redisStore。
app.use(session({
secret: 'ssshhhhh',
// create new redis store.
store: new redisStore({ host: 'localhost', port: 6379, client: client,ttl : 260}),
saveUninitialized: false,
resave: false
}));
I put ttl to 260, you can increase. After TTL reaches its limits, it will automatically delete the redis key.
我把ttl设为260,你可以增加。在TTL达到其限制后,它将自动删除redis键。
In routers you can use req.session variable to SET, EDIT or DESTROY the session.
在路由器中,你可以使用req。会话变量设置、编辑或销毁会话。
One more thing...
一件事……
If you want custom cookie i.e not as same as in your Redis store you can use cookie-parser to set cookie secrets.
如果你想要定制cookie i。与在Redis存储中不同,您可以使用cookie-parser来设置cookie秘密。
Hope it helps.
希望它可以帮助。
link : https://codeforgeek.com/2015/07/using-redis-to-handle-session-in-node-js/
链接:https://codeforgeek.com/2015/07/using-redis-to-handle-session-in-node-js/
#1
21
That should be all there is to it. You access the session in your route handlers via req.session
. The sessions are created, saved, and destroyed automatically.
这就是一切。通过req.session访问路由处理程序中的会话。这些会话是自动创建、保存和销毁的。
If you need to manually create a new session for a user, call req.session.regenerate()
.
如果您需要手动为用户创建一个新的会话,请调用req.session. rebuild()。
If you need to save it manually, you can call req.session.save()
.
如果需要手动保存,可以调用req.session.save()。
If you need to destroy it manually, you can call req.session.destroy()
.
如果需要手动销毁,可以调用req.session.destroy()。
See the Connect documentation for the full list of methods and properties.
有关方法和属性的完整列表,请参阅连接文档。
#2
5
Consider this code.
考虑这段代码。
var express = require('express');
var redis = require("redis");
var session = require('express-session');
var redisStore = require('connect-redis')(session);
var bodyParser = require('body-parser');
var client = redis.createClient();
var app = express();
app.set('views', __dirname + '/views');
app.engine('html', require('ejs').renderFile);
app.use(session({
secret: 'ssshhhhh',
// create new redis store.
store: new redisStore({ host: 'localhost', port: 6379, client: client,ttl : 260}),
saveUninitialized: false,
resave: false
}));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: true}));
app.get('/',function(req,res){
// create new session object.
if(req.session.key) {
// if email key is sent redirect.
res.redirect('/admin');
} else {
// else go to home page.
res.render('index.html');
}
});
app.post('/login',function(req,res){
// when user login set the key to redis.
req.session.key=req.body.email;
res.end('done');
});
app.get('/logout',function(req,res){
req.session.destroy(function(err){
if(err){
console.log(err);
} else {
res.redirect('/');
}
});
});
app.listen(3000,function(){
console.log("App Started on PORT 3000");
});
So you need to install connect-redis and pass your express-session instance to it.
因此,您需要安装connectredis并将您的express-session实例传递给它。
Then in middleware initialize redisStore with server details like this.
然后在中间件中使用服务器细节初始化redisStore。
app.use(session({
secret: 'ssshhhhh',
// create new redis store.
store: new redisStore({ host: 'localhost', port: 6379, client: client,ttl : 260}),
saveUninitialized: false,
resave: false
}));
I put ttl to 260, you can increase. After TTL reaches its limits, it will automatically delete the redis key.
我把ttl设为260,你可以增加。在TTL达到其限制后,它将自动删除redis键。
In routers you can use req.session variable to SET, EDIT or DESTROY the session.
在路由器中,你可以使用req。会话变量设置、编辑或销毁会话。
One more thing...
一件事……
If you want custom cookie i.e not as same as in your Redis store you can use cookie-parser to set cookie secrets.
如果你想要定制cookie i。与在Redis存储中不同,您可以使用cookie-parser来设置cookie秘密。
Hope it helps.
希望它可以帮助。
link : https://codeforgeek.com/2015/07/using-redis-to-handle-session-in-node-js/
链接:https://codeforgeek.com/2015/07/using-redis-to-handle-session-in-node-js/