Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node.js Express disable automatic session creation

If I enable the session feature of express via app.use(express.session({secret: "12345"})); the session cookie is set when the user first hits a page.

How can I disable this behavior and decide manually when to create a cookie, for example after a successful login? I am aware that I could just construct a cookie-header manually, but I would like to stay with express.session.

like image 999
user3033490 Avatar asked Oct 20 '22 17:10

user3033490


1 Answers

Define the session support as middleware, but don't use use:

var sessions = express.session({
    // etc
});

...

app.get('/', function (req, resp) {
    // No session
});

app.post('/user', sessions, function (req, resp) {
    // Has sessions
like image 166
7zark7 Avatar answered Oct 27 '22 09:10

7zark7