Launch offer
Business website $150 USD Custom plugin $200 USD Ready in 5 days
Get a quote
Node.js

Sessions in Node.js and Express

Manage server-side sessions with express-session for login state across requests.

Sessions in Node.js and Express — a practical guide to Express sessions with clear examples you can reuse in real projects.

Sessions in Node.js and Express — 51/55 in the Node.js series. Full playlist companion: Node.js complete course on YouTube. Prefer one article? Read the complete Node.js tutorial.

Short description

Sessions store data on the server and give the browser a session ID cookie. Useful for classic server-rendered login flows.

express-session

const session = require('express-session');

app.use(session({
  secret: process.env.SESSION_SECRET,
  resave: false,
  saveUninitialized: false,
}));

app.post('/login', (req, res) => {
  req.session.userId = 1;
  res.send('logged in');
});

Leave a reply

Your email address will not be published. Required fields are marked *