Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
556 views
in Technique[技术] by (71.8m points)

node.js - How to save and retrieve session from Redis

I am trying to integrate Redis sessions into my authentication system written in Node.js.

I have been able to successfully set up Redis server, connect-redis and Express server.

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

I am aware that that is probably extremely simple, but please don't downvote and just explain :).

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

If you need to manually create a new session for a user, call req.session.regenerate().

If you need to save it manually, you can call req.session.save().

If you need to destroy it manually, you can call req.session.destroy().

See the Connect documentation for the full list of methods and properties.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...