Since connect doesn't use the parseCookie method anymore, how can we get session data using express?

前端 未结 4 882
天命终不由人
天命终不由人 2021-01-02 00:22

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<
4条回答
  •  忘掉有多难
    2021-01-02 01:07

    Your questions:

    1. How can I update/change a cookie through RedisStore?
    2. It looks like session data is saved only in cookies. How can I keep track of user information from page to page if someone has cookies turned off?

    Cookies / Sessions / RedisStore Thoughts:

    1. Typically, you have exactly one cookie, which is the session id
    2. All user-state is stored on the server in a "session" which can be found via the session id
    3. You can use Redis as your back-end storage for your session data.
    4. Redis will allow you to keep session state, even when your server is restarted (good thing)
    5. You can store a mountain of data in your session (req.session.key = value)
    6. All data stored in the session will be persistant until the user logs out, or their session expires

    Example node.js Code:

    var app = express.createServer(
      express.static(__dirname + '/public', { maxAge: 31557600000 }),
      express.cookieParser(),
        express.session({ secret: 'secret', store: new RedisStore({
              host: 'myredishost',
              port: 'port',
              pass: 'myredispass',
              db: 'dbname',
          }, cookie: { maxAge: 600000 })})
    );
    

    Session and Cookie Thoughts:

    1. Your second issue us about sessions without cookies. This is possible.
    2. You, basically, put the session id on the url of every request you send to the server.
    3. I strongly believe that most people allow cookies.
    4. If this is a requirement, google: "session without cookies"

提交回复
热议问题