Middleware is a function that runs during the request-response cycle. It can modify the request or response objects, end the request, or pass control to the next middleware.
Click to reveal answer
beginner
How does middleware execution flow work in Express.js?
Middleware functions run in the order they are added. Each middleware calls next() to pass control to the next one. If next() is not called and the response is not ended, the request stops there.
Click to reveal answer
intermediate
What happens if a middleware does not call next()?
The request-response cycle stops at that middleware. The server will not continue to the next middleware or route handler, possibly causing the request to hang.
Click to reveal answer
beginner
What are common uses of middleware in Node.js?
Middleware is used for logging, authentication, parsing request bodies, error handling, and serving static files.
Click to reveal answer
intermediate
Explain the difference between application-level and error-handling middleware.
Application-level middleware handles normal requests and calls next() to continue. Error-handling middleware has four parameters (err, req, res, next) and handles errors passed down the chain.
Click to reveal answer
What does the next() function do in middleware?
AEnds the request-response cycle
BThrows an error
CSends a response to the client
DPasses control to the next middleware
✗ Incorrect
Calling next() passes control to the next middleware or route handler in the stack.
Which of these is NOT a typical use of middleware?
ARendering HTML templates
BParsing JSON bodies
CHandling authentication
DLogging requests
✗ Incorrect
Rendering HTML is usually done in route handlers or view engines, not middleware.
What happens if middleware does not call next() or end the response?
AThe request continues normally
BThe request hangs and never completes
CThe server crashes
DAn error is automatically thrown
✗ Incorrect
Without calling next() or ending the response, the request stays open and the client waits indefinitely.
How many parameters does an error-handling middleware function have?
A2
B3
C4
D1
✗ Incorrect
Error-handling middleware has four parameters: (err, req, res, next).
In Express.js, middleware functions are executed in:
AThe order they are added
BRandom order
CAlphabetical order
DReverse order
✗ Incorrect
Middleware runs in the order they are added to the app or router.
Describe what middleware is and how it controls the flow of requests in a Node.js app.
Think about how functions can be chained to handle requests step-by-step.
You got /4 concepts.
Explain the difference between normal middleware and error-handling middleware in Express.js.
Focus on the special signature and role of error middleware.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of middleware in a Node.js Express app?
easy
A. To style the HTML pages
B. To directly send responses to the client without routes
C. To define database schemas
D. To run code on requests before they reach the route handlers
Solution
Step 1: Understand middleware role and compare options
Middleware functions run before route handlers to process requests. Only To run code on requests before they reach the route handlers describes middleware's role correctly; others describe unrelated tasks.
Final Answer:
To run code on requests before they reach the route handlers -> Option D
Quick Check:
Middleware = Pre-route code [OK]
Hint: Middleware runs before routes to handle requests [OK]
Common Mistakes:
Thinking middleware sends responses directly
Confusing middleware with database logic
Assuming middleware styles pages
2. Which of the following is the correct way to define a middleware function in Express?
easy
A. app.get((req, res) => { console.log('Middleware'); });
B. app.use((req, res, next) => { console.log('Middleware'); next(); });
C. app.middleware((req, res, next) => { next(); });
D. app.route('/').middleware((req, res) => { next(); });
Solution
Step 1: Recall Express middleware syntax and check options
Middleware uses app.use() with (req, res, next) parameters. Only app.use((req, res, next) => { console.log('Middleware'); next(); }); uses app.use() and calls next(), which is correct middleware syntax.
What will be printed in the console when a GET request is made to '/'?
medium
A. First\nSecond
B. Second\nFirst
C. Done
D. No output
Solution
Step 1: Understand middleware execution order and route handler
Middleware runs in the order added: first prints 'First', then calls next(), second prints 'Second'. After middleware, route handler sends response but does not print to console.
Final Answer:
First\nSecond -> Option A
Quick Check:
Middleware order = console logs order [OK]
Hint: Middleware logs appear in order added before route runs [OK]
D. It uses wrong parameters in middleware function
Solution
Step 1: Check middleware function and request flow effect
The middleware logs 'Start' but does not call next(), so it blocks further processing. Without next(), the route handler never runs, so the request hangs.
Final Answer:
It never calls next(), so the request hangs -> Option C
Quick Check:
Missing next() blocks request [OK]
Hint: Always call next() in middleware unless sending response [OK]
Common Mistakes:
Forgetting to call next() in middleware
Calling next() multiple times
Confusing middleware parameters
5. You want to add two middleware functions to log request time and check authentication before handling routes. Which code correctly ensures both run in order and the route handler executes after?