Asked 30+ days ago
13 Feb 2017
Views 369
lain

lain posted

how to use session in express framework ?

working with Node JS Express Framework - web app, but do not know how to set session for authentication of admin
Rasi

Rasi
answered Dec 17 '17 20:17

use express-session

Install express-session

npm install express-session


initialize express-session

var session = require('express-session')



how to use session in express framework ?


var app = express()
app.set('trust proxy', 1) // trust first proxy
app.use(session({
  secret: 'keyboard cat',
  resave: false,
  saveUninitialized: true,
  cookie: { secure: true }
}))



Edit Answer
Share
Phpworker

Phpworker
answered Dec 17 '17 20:17

use database to store session in express framework

use MySQL to store session in express framework

Install express-mysql-session


npm install express-mysql-session 


how to use mysql session in express framework ?


var express = require('express');
var app = module.exports = express();
var session = require('express-session');
var MySQLStore = require('express-mysql-session')(session);

var options = {
    host: 'localhost',
    port: 3306,
    user: 'session_test',
    password: 'password',
    database: 'session_test'
};

var sessionStore = new MySQLStore(options);

app.use(session({
    key: 'session_cookie_name',
    secret: 'session_cookie_secret',
    store: sessionStore,
    resave: true,
    saveUninitialized: true
}));


use MongoDB to store session in express framework


Install express-session-mongo


npm install https://github.com/trottski/express-session-mongo/archive/master.tar.gz


how to use MongoDB to store session in express framework ?

 
var xp = require('express'),
    MongoStore = require('express-session-mongo');

var app = xp.createServer();

app.configure(function(){
    app.use(xp.cookieDecoder());
    app.use(xp.session({ store: new MongoStore() }));
    app.use(app.router);
});

Edit Answer
Share
shyam

shyam
answered Dec 17 '17 20:17

i used node-session for on of website based on express framework

node-session install command

npm install node-session


start coding to initialize the session

var NodeSession = require('node-session');
session = new NodeSession({secret: 'Q3UBzdH9GEfiRCTKbi5MTPyChpzXLsTD'});


node-session
Set session by Node JS in express framework

session.startSession(req, res);
req.session.put('userId', rows[0].id);


get session value by Node JS in express framework

session.startSession(req, res);
var UserId = req.session.get('userId');
 


one can use file / memory / database to store session in Node js by node-session
Edit Answer
Share
Post Answer