Preserving session between node.js server reboots -


i'm developing node.js app , use nodemon run it. question how keep session open when make change in server, , nodemon reboot.

this answer assumes you're using express or koa, 2 of more popular node.js web frameworks.

in application, have line says following.

express 4

var app = require( 'express' )() var session = require( 'express-session' ) app.use( session() ) // session middleware 

koajs

var app = require( 'koa' )() var session = require( 'koa-session' ) app.use( session() ) // session middleware 

the guiding wisdom in web applications make world-facing side of stateless possible. means node.js server holds no information between requests , can destroyed , restarted anytime.

a session stateful thing. because of this, need store session data in designed keep data uncorrupted in long run. commonly done through database (more secure) or browser cookie (less secure).

the express-session module holds session information in memory default. violates stateless ideals , lose session information on reboot.

the koa-session module uses cookies default. stateless, raises security concerns in may possible users modify own session variables, don't use billing or other sensitive operations.

in both of above modules, can override defaults own session store.

redis session store

i store session data in database redis.

express

this can wired express module connect-redis.

quoting readme:

var session = require('express-session'); var redisstore = require('connect-redis')(session);  app.use(session({     store: new redisstore(options),     secret: 'keyboard cat' })); 

koajs

using redis store session data possible in koa koa-redis module. paraphrasing readme:

var app require('koa')(); var session = require('koa-generic-session'); var redisstore = require('koa-redis');  app.keys = ['keys', 'keykeys'];  app.use(session({   store: redisstore() }));  app.use(function *() {   this.session.name = 'koa-redis'; }); 

no databases session store

express

express can store session data in cookies cookie-session extension.

koa

as mentioned before, koa-session module stores sessions in cookies default.


Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -