由于连接不再使用parseCookie方法,我们如何使用express获取会话数据? [英] Since connect doesn't use the parseCookie method anymore, how can we get session data using express?
问题描述
在node.js和express中,有很多示例显示如何获取会话数据。
In node.js and express, there are many examples showing how to get session data.
- Node.js and Socket.io
- Express and Socket.io - Tying it all Together
- Socket.io and Session?
可以看到当你访问第三个链接,它是一个到StackOverflow的链接。有一个很好的答案,但正如@UpTheCreek的评论所指出的,连接不再具有parseCookie方法。我也遇到了这个问题。我发现的所有教程都使用了连接的 parseCookie
方法,现在不存在。所以我问他如何获得会话数据,他说他不知道最好的方法,所以我以为我会在这里发布问题。当使用 express@3.0.0rc4
, socket.io
和 redis
,我们如何获取会话数据并使用它来授权用户?我已经能够使用 require('connect')。utils.parseSignedCookie;
,但是当我这样做时,我总是在握手时收到一个警告/错误, p>
As you can see when you visit the 3rd link, it's a link to StackOverflow. There was a good answer, but as pointed out in those comments by @UpTheCreek, connect no longer has the parseCookie method. I have just run into this problem as well. All of the tutorials I have found uses connect's parseCookie
method which now doesn't exist. So I asked him how we can get the session data and he said he doesn't know the best approach so I thought I'd post the question here. When using express@3.0.0rc4
, socket.io
, and redis
, how can we get session data and use that to authorize the user? I've been able to use require('connect').utils.parseSignedCookie;
, but when I do that, I always get a warning/error when handshaking,
warn - handshake error Error
,从我看过的它听起来不是一个永久的解决方案。
更新
and from what I've read it sounds like that isn't a permanent solution anyways.
UPDATE
确定我有 session.socket.io
在我的服务器上工作。当我怀疑,我被困在授权点。我想我可能会这样做错了,所以随时纠正我。在我的Redis数据库中,我将有用户的信息。他们第一次登录,我想更新他们的cookie,所以它包含他们的用户信息。那么下一次他们回到网站时,我想检查他们是否有一个cookie,如果用户信息在那里。如果没有,我想把它们发送到登录屏幕。在登录屏幕上,当用户提交信息时,它将根据Redis数据库测试该信息,如果匹配,它将使用用户信息更新cookie。我的问题是:
Ok I got session.socket.io
working on my server. And as I suspected, I got stuck at the point of authorizing. I think I might be going about this the wrong way, so feel free to correct me. In my Redis database, I will have user's information. The first time that they login, I want to update their cookie so it contains their user information. Then the next time they come back to the site, I want to check if they have a cookie and if the user information is there. If it is not there, I want to send them to the login screen. At the login screen, when a user submits information, it would test that information against the Redis database, and if it matches, it would update the cookie with user information. My questions are these:
1)如何通过RedisStore更新/更改cookie?
2)看起来会话数据仅保存在Cookie中。如果有人关闭cookies,我如何跟踪用户信息?
以下是我适用的代码:
//...hiding unapplicable code...
var redis = require('socket.io/node_modules/redis');
var client = redis.createClient();
var RedisStore = require('connect-redis')(express);
var redis_store = new RedisStore();
var cookieParser = express.cookieParser('secret');
app.configure(function(){
//...hiding unapplicable code...
app.use(cookieParser);
app.use(express.session({secret: 'secret', store: redis_store}));
});
//...hiding code that starts the server and socket.io
var SessionSockets = require('session.socket.io');
var ssockets = new SessionSockets(io, redis_store, cookieParser);
io.configure(function(){
io.set('authorization', function(handshake, callback){
if(handshake.headers.cookie){
//var cookie = parseCookie(handshake.headers.cookie);
//if(cookie.user){
// handshake.user = cookie.user;
//}
}
callback(null, true);
});
});
ssockets.on('connection', function(err, socket, session){ ... });
推荐答案
切换到会话后。 socket.io
一段时间我遇到了一些问题,由于模块在加载会话信息时的异步性质。所以我最终创建了自己的模块,名为 session.io 。它被这样使用:
After switching to session.socket.io
for a while I ran into a few problems due to the asynchronous nature of the module when loading the session information. So I ended up creating my own module called session.io. It is used like this:
var express = require('express');
var app = express();
var server = require('http').createServer(app);
//Setup cookie and session handlers
//Note: for sessionStore you can use any sessionStore module that has the .load() function
//but I personally use the module 'sessionstore' to handle my sessionStores.
var cookieParser = express.cookieParser('secret');
var sessionStore = require('sessionstore').createSessionStore();
app.configure(function(){
app.set('port', process.env.PORT || 3000);
//...truncate...//
app.use(cookieParser);
//make sure to use the same secret as you specified in your cookieParser
app.use(express.session({secret: 'secret', store: sessionStore}));
app.use(app.router);
});
app.get('/', function(req, res){
res.send('<script src="/socket.io/socket.io.js"></script><script>io.connect();</script>Connected');
});
server.listen(app.get('port'), function(){
console.log('Listening on port ' + app.get('port'));
});
var io = require('socket.io').listen(server);
io.configure(function(){
//use session.io to get our session data
io.set('authorization', require('session.io')(cookieParser, sessionStore));
});
io.on('connection', function(socket){
//we now have access to our session data like so
var session = socket.handshake.session;
console.log(session);
});
这篇关于由于连接不再使用parseCookie方法,我们如何使用express获取会话数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!