Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to add a middleware function that logs every request.
Express
app.use(function(req, res, [1]) { console.log('Request received'); [1](); });
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a wrong parameter name instead of
next causes the middleware chain to break.✗ Incorrect
The next parameter is used to pass control to the next middleware function.
2fill in blank
mediumComplete the code to create a middleware that handles JSON body parsing.
Express
app.use(express.[1]()); Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using
urlencoded instead of json when expecting JSON data.✗ Incorrect
The express.json() middleware parses incoming JSON requests.
3fill in blank
hardFix the error in the middleware that does not call the next function.
Express
app.use(function(req, res, [1]) { res.send('Hello'); });
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Omitting
next() causes the request to hang.✗ Incorrect
Middleware must call next() to pass control unless it ends the response.
4fill in blank
hardFill both blanks to create a middleware that checks if user is authenticated and calls next if true.
Express
app.use(function(req, res, [1]) { if(req.user) { [2](); } else { res.status(401).send('Unauthorized'); } });
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong parameter names or forgetting to call
next().✗ Incorrect
The middleware uses next to continue if the user is authenticated.
5fill in blank
hardFill all three blanks to create a middleware that logs method and URL, then calls next.
Express
app.use(function(req, res, [1]) { console.log(req.[2], req.[3]); [1](); });
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong property names or forgetting to call
next().✗ Incorrect
This middleware logs the HTTP method and URL, then calls next() to continue.