既然 connect 不再使用 parseCookie 方法,那么我们如何使用 express 获取会话数据呢? [英] Since connect doesn't use the parseCookie method anymore, how can we get session data using express?

查看:17
本文介绍了既然 connect 不再使用 parseCookie 方法,那么我们如何使用 express 获取会话数据呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 node.js 和 express 中,有很多示例展示了如何获取会话数据.

In node.js and express, there are many examples showing how to get session data.

正如您在访问第三个链接时所看到的,它是一个指向 StackOverflow 的链接.有一个很好的答案,但正如@UpTheCreek 在这些评论中指出的那样,connect 不再具有 parseCookie 方法.我也刚刚遇到了这个问题.我发现的所有教程都使用 connect 的 parseCookie 方法,该方法现在不存在.所以我问他我们如何获得会话数据,他说他不知道最好的方法,所以我想我会在这里发布这个问题.当使用express@3.0.0rc4socket.ioredis时,我们如何获取会话数据并使用它来授权用户?我已经能够使用 require('connect').utils.parseSignedCookie;,但是当我这样做时,我总是在握手时收到警告/错误,

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 中.如果有人关闭了 Cookie,我如何逐页跟踪用户信息?

这是我的适用代码:

//...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){ ... });

推荐答案

在切换到 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);
});

这篇关于既然 connect 不再使用 parseCookie 方法,那么我们如何使用 express 获取会话数据呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆