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 });
});