Quiz Entry - updated: 2026.06.20
How do you read and write session variables in Express.js?
Treat req.session as a plain object whose properties persist across requests for the same session.
Setting a value:
function (req, res) {
req.session.lastOption = 4;
}
Reading a value:
function (req, res) {
if (req.session.hasOwnProperty('lastOption')) {
let value = req.session.lastOption;
}
}
Session variables persist across requests for the same session.