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

Cookies in Node.js and Express

Set and read HTTP cookies in Express for preferences and lightweight client state.

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

Cookies in Node.js and Express — 50/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

Cookies are small key/value pairs stored by the browser and sent on later requests. Prefer `httpOnly` cookies for tokens when appropriate.

cookie-parser

const cookieParser = require('cookie-parser');
app.use(cookieParser());

app.get('/set', (req, res) => {
  res.cookie('theme', 'dark', { httpOnly: true, maxAge: 86400000 });
  res.send('cookie set');
});

app.get('/get', (req, res) => {
  res.json({ theme: req.cookies.theme });
});

Leave a reply

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