What's difference with express-session and cookie-session?

后端 未结 7 1300
既然无缘
既然无缘 2021-01-30 00:00

I am new with Express. As Express 4.x has removed bundled middlewares. Any middleware I want to use should be required. When I read the README with exp

相关标签:
7条回答
  • 2021-01-30 00:30

    express-session stores the session identifier in the cookie while the actual session data resides in backend session store like connect-redis, where as cookie-session allows you to store the session data in a cookie (client-side).

    From the documentation of cookie-session:

    A user session can be stored in two main ways with cookies: on the server or on the client. This module stores the session data on the client within a cookie, while a module like express-session stores only a session identifier on the client within a cookie and stores the session data on the server, typically in a database.

    The main advantage of using cookie-session is when you have a clustered node.js app, then you don't have to rely on sharing session data between forked processes.

    0 讨论(0)
  • 2021-01-30 00:35

    v4-> cookie-session is (Establish cookie-based sessions.) equals in ->v3 express.cookieSession

    v4-> express-session is (Establish server-based sessions (development only)). equals in ->v3 express.session

    0 讨论(0)
  • 2021-01-30 00:38

    The official Express.js documentation refers to

    The main difference between these two modules is how they save cookie session data.

    The express-session middleware stores session data on the server; it only saves the session ID in the cookie itself, not session data. By default, it uses in-memory storage and is not designed for a production environment. In production, you’ll need to set up a scalable session-store; see the list of compatible session stores.

    In contrast, cookie-session middleware implements cookie-backed storage: it serializes the entire session to the cookie, rather than just a session key. Only use it when session data is relatively small and easily encoded as primitive values (rather than objects). Although browsers are supposed to support at least 4096 bytes per cookie, to ensure you don’t exceed the limit, don’t exceed a size of 4093 bytes per domain. Also, be aware that the cookie data will be visible to the client, so if there is any reason to keep it secure or obscure, then express-session may be a better choice.

    0 讨论(0)
  • 2021-01-30 00:40

    The get a non-empty console.log(req.session) you need to set session values before logging.

    From the cookie-session repo (https://github.com/expressjs/cookie-session):

    app.get('/', function (req, res, next) {
     req.session.views = (req.session.views || 0) + 1
     console.log(req.session)
     res.end(req.session.views + ' views')
    })
    

    If you never set any info on the req.session object, it will return empty.

    0 讨论(0)
  • 2021-01-30 00:45

    Basically, express-session is more abstract, it supports different session stores (like files, DB, cache and whatnot).

    And cookie-session is a simple / lightweight cookie-based (cookie is the only storage engine supported: all the session info is stored on the client, in a cookie) session implementation. This kind of sessions is probably most famous for its Rails implementation.

    0 讨论(0)
  • 2021-01-30 00:49

    The basic difference between both these relates to how and where is the session data being stored. Cookie session is basically used for lightweight session applications where the session data is stored in a cookie but within the client [browser], whereas, Express Session stores just a mere session identifier within a cookie in the client end, whilst storing the session data entirely on the server. Cookie Session is helpful in applications where no database is used in the back-end. However, the session data cannot exceed the cookie size. On conditions where a database is used, it acts like a cache to stop frequent database lookups which is expensive.

    0 讨论(0)
提交回复
热议问题