Sessions in Node JS

前端 未结 10 799
北海茫月
北海茫月 2021-01-07 23:46

How can I maintain my SESSIONS in Node JS ? E.g I want to store UserID in SESSION using Node Js. How can I do that in Node JS ? And can I use that Node JS SESSION in PHP too

10条回答
  •  醉梦人生
    2021-01-08 00:17

    ExpressJS has official session middleware, it is also the current de-facto standard web framework for Node.js.


    If you wish to implement session support on your own, this is how the implementation is normally done, upon every request:

    1. Check if cookie contains a session ID
      • If not, create a session object that is either stored in memory, on file, or in a database (or a combination of those), and set the session id in the response cookie to match this object's identifier.
      • If the cookie does contain a session ID, locate the session object by the ID.
    2. Provide the obtained/created object from step 1 as the persisted session object for the request.

    You will also have to implement some timeout mechanism, so that after a while the session objects are deleted, at least from memory.

提交回复
热议问题