Complete the code to import the Express library.
const express = require('[1]');
The Express library is imported using require('express'). This allows us to create an Express app.
Complete the code to create a new Express app instance.
const app = [1]();Calling express() creates a new Express application instance.
Fix the error in the middleware that checks if a user is authenticated.
function checkAuth(req, res, next) {
if (!req.user) {
return res.status([1]).send('Unauthorized');
}
next();
}The HTTP status code 401 means 'Unauthorized', which is correct when a user is not authenticated.
Fill both blanks to create a route that requires authentication before sending a response.
app.[1]('/dashboard', [2], (req, res) => { res.send('Welcome to your dashboard'); });
The route uses get to respond to GET requests and checkAuth middleware to ensure the user is authenticated.
Fill all three blanks to set up a login route that authenticates user credentials and sends a success message.
app.[1]('/login', (req, res) => { const { username, password } = req.[2]; if (username === 'admin' && password === 'secret') { res.[3]('Login successful'); } else { res.status(401).send('Invalid credentials'); } });
The login route uses POST method, reads credentials from req.body, and sends a response with res.send().