0
0
Node.jsframework~10 mins

Why middleware is fundamental in Node.js - 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.

Node.js
app.use(function(req, res, next) { console.log(req.[1]); next(); });
Drag options to blanks, or click blank then click option'
Aurl
Bmethod
Cbody
Dheaders
Attempts:
3 left
💡 Hint
Common Mistakes
Using req.method logs the HTTP method, not the URL.
2fill in blank
medium

Complete the code to create middleware that ends the response with 'Hello World'.

Node.js
app.use(function(req, res, [1]) { res.send('Hello World'); });
Drag options to blanks, or click blank then click option'
Aclose
Bend
Cfinish
Dnext
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'end' or 'finish' instead of 'next' causes errors.
3fill in blank
hard

Fix the error in the middleware that should call the next function.

Node.js
app.use(function(req, res, next) { console.log('Request received'); [1](); });
Drag options to blanks, or click blank then click option'
Ares.send
Bnext
Creq.next
Dres.end
Attempts:
3 left
💡 Hint
Common Mistakes
Calling res.end() or res.send() instead of next() stops middleware chain.
4fill in blank
hard

Fill both blanks to create middleware that checks if user is authenticated and calls next if true.

Node.js
app.use(function(req, res, next) { if (req.[1]) { [2](); } else { res.status(401).send('Unauthorized'); } });
Drag options to blanks, or click blank then click option'
AisAuthenticated
Bnext
Csend
Dstatus
Attempts:
3 left
💡 Hint
Common Mistakes
Calling res.send() instead of next() stops the chain.
5fill in blank
hard

Fill all three blanks to create middleware that logs method, URL, and then calls next.

Node.js
app.use(function(req, res, next) { console.log(req.[1] + ' ' + req.[2]); [3](); });
Drag options to blanks, or click blank then click option'
Amethod
Burl
Cnext
Dbody
Attempts:
3 left
💡 Hint
Common Mistakes
Logging req.body instead of req.url or forgetting to call next().