0
0
Expressframework~10 mins

Why middleware is Express's core concept - Test Your Understanding

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete 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'
Acallback
Bnext
Cdone
Dfinish
Attempts:
3 left
💡 Hint
Common Mistakes
Using a wrong parameter name instead of next causes the middleware chain to break.
2fill in blank
medium

Complete 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'
Ajson
Brouter
Cstatic
Durlencoded
Attempts:
3 left
💡 Hint
Common Mistakes
Using urlencoded instead of json when expecting JSON data.
3fill in blank
hard

Fix 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'
Anext
Bdone
Ccallback
Dfinish
Attempts:
3 left
💡 Hint
Common Mistakes
Omitting next() causes the request to hang.
4fill in blank
hard

Fill 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'
Anext
Bdone
Dcallback
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong parameter names or forgetting to call next().
5fill in blank
hard

Fill 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'
Anext
Bmethod
Curl
Dcallback
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong property names or forgetting to call next().