Express JS Save Session on Restart

I am trying to restart and maintain a session using nodemon, but when I restart, the session is destroyed .. I wonder if there is a way to continue living with nodemon or another node js library session.

scripts ": {//" Start ":" node./bin/www "" start ":" nodemon./bin/www "}

thank you for your time

+6
source share
2 answers

To save a session, there are two approaches:

  • Use some persistent storage

  • Use JSON Tokens

To implement a persistent session, you can use the MongoDB session store or the Redis session store.

If you want to use redis, use the connect-redis npm package. If you want to use MongoDb as a session repository, use the connect-mongo npm package

There are several settings that you need to make in the app.js / server.js application. In one of my demos, I use the Redis Session store with PassportJS, if you are looking for an example, feel free to look here .

If you want to use JSON tags for websites , many different implementations are available. I am using jsonwebtoken . I implemented this using PassportJS, ExpressJS and AngularJS in front of End. For example, look here . Tokens are encoded and stored in the local storage of the browser using a secret key.

I would suggest you go for JSON tokens, read it in detail, because this is how most major web applications develop. Let me know if you need more help.

+4
source

Your session is good only until the process disappears. Then it is destroyed. You should use a persistent storage engine or a simple solution:

https://github.com/expressjs/cookie-session

It may not be a good long-term solution, but it will help you get started.

+2
source

Source: https://habr.com/ru/post/983431/


All Articles