Complete the code to import the session middleware in a NestJS app.
import * as session from '[1]';
The express-session package is used to manage sessions in a NestJS app.
Complete the code to apply session middleware in the main app module.
app.use(session({ secret: '[1]', resave: false, saveUninitialized: false }));The secret is a string used to sign the session ID cookie. 'keyboard cat' is a common example.
Fix the error in the session configuration to avoid saving uninitialized sessions.
app.use(session({ secret: 'keyboard cat', resave: false, saveUninitialized: [1] }));Setting saveUninitialized to false prevents saving empty sessions, improving performance and privacy.
Fill both blanks to configure cookie settings for secure sessions.
app.use(session({ secret: 'keyboard cat', cookie: { secure: [1], maxAge: [2] } }));For development, secure is false to allow HTTP. maxAge sets cookie life in milliseconds; 3600000 is 1 hour.
Fill all three blanks to create a session-based login route that saves user info.
app.post('/login', (req, res) => { const { username } = req.body; req.session.[1] = [2]; res.[3]('Logged in'); });
The session property user stores the username. The response uses send to reply with a message.