Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Express JS Keep Session Alive On restart

I'm trying to restart and keep the session alive using nodemon, but when you restart the session is destroyed.. I wonder if there is any way to keep living with nodemon or other node js library session.

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

Thanks for your time

like image 625
josemm1790 Avatar asked Mar 06 '15 15:03

josemm1790


1 Answers

In order to persist session, there are two approaches:

  1. Use some persistent store

  2. Use JSON Web Tokens

For implementing persistent session, you can use MongoDB session store or Redis Session store.

If you want to use redis then make use of connect-redis npm package. If you want to use MongoDb as session store then make use of connect-mongo npm package

There are some settings which you need to do in you app.js/server.js. In one of my demo i am using Redis Session store with PassportJS, if you are looking for example, feel free to look here.

If you want to use JSON web tokens, there are many different implementations available. I am using jsonwebtoken. I implemented this using PassportJS, ExpressJS and AngularJS in front End. For example look here. Tokens are encoded and stored in browser's local storage with a secret key.

I would suggest you to go for JSON web tokens, read it in detail because that is how most of the major web apps are developed. Let me know if you need more help.

like image 188
NarendraSoni Avatar answered Oct 15 '22 20:10

NarendraSoni