Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to create different session on url based in nodejs?

Our site have two section :

1. admin (content management)  
2. site

Admin section will have url '/admin' and site section have url '/'.

Example : My site name is www.example.com, then admin section url is "www.example.com/admin" and site url is "www.example.com".

I want to create url based session means request from admin section will have different session than request from site section.

How can i do it?

like image 841
rajeshpanwar Avatar asked Oct 31 '22 11:10

rajeshpanwar


1 Answers

Assuming you're using express, you can separate your routers for your routes and apply the session middleware to each router.

routes/index.js

var express = require('express'),
    router = express.Router(),
    session = require('express-session');

router.use(session({
    secret: 'index route secret',
    resave: false,
    saveUninitialized: true
}));

module.exports = router;

routes/admin.js

var express = require('express'),
    router = express.Router(),
    session = require('express-session');

router.use(session({
    secret: 'admin route secret',
    resave: false,
    saveUninitialized: true
}));

module.exports = router;

app.js

var express = require('express'),
    app = express();

app.use('/', require('./routes/index'));
app.use('/admin', require('./routes/admin'));

app.listen(3000);

documentation

like image 192
Ben Fortune Avatar answered Nov 15 '22 06:11

Ben Fortune