Middleware is a function that runs during the request-response cycle. It can modify the request or response, end the request, or pass control to the next middleware.
Click to reveal answer
beginner
Why does the order of middleware matter in Express?
Middleware runs in the order it is added. If the order is wrong, some middleware might not run or might run too late, causing bugs or unexpected behavior.
Click to reveal answer
intermediate
What happens if a middleware does not call next()?
If a middleware does not call next(), the request stops there and no further middleware or route handlers run. This can cause the request to hang.
Click to reveal answer
intermediate
How can middleware ordering affect authentication?
Authentication middleware should run before routes that need protection. If it runs after, unauthorized users might access protected routes.
Click to reveal answer
beginner
Give an example of middleware ordering in Express.
Example: app.use(logger); app.use(auth); app.get('/profile', profileHandler); Logger runs first, then auth, then the route handler.
Click to reveal answer
In Express, what happens if middleware is placed after the route handler?
AThe middleware runs before the route handler
BThe middleware will not run for that route
CThe middleware runs twice
DThe middleware automatically moves before the route
✗ Incorrect
Middleware runs in the order it is added. If placed after a route handler, it won't run for that route.
What must middleware do to pass control to the next middleware?
AThrow an error
BReturn true
CCall res.send()
DCall next() function
✗ Incorrect
Middleware must call next() to pass control to the next middleware or route handler.
Why should authentication middleware run early in the middleware chain?
ATo protect routes from unauthorized access
BTo speed up the server
CTo log requests
DTo handle errors
✗ Incorrect
Running authentication early ensures only authorized users access protected routes.
What is a common use of middleware in Express?
ALogging requests
BCompiling CSS
CRendering HTML templates
DConnecting to databases
✗ Incorrect
Middleware often logs requests to track server activity.
If middleware does not call next() or end the response, what happens?
AServer crashes
BRequest automatically ends
CRequest hangs and never completes
DNext middleware runs anyway
✗ Incorrect
Without calling next() or ending response, the request stays open and hangs.
Explain why middleware order is important in Express and give an example.
Think about how requests flow through middleware functions.
You got /4 concepts.
Describe what happens if a middleware forgets to call next() and why this matters.
Consider the flow of control in Express middleware.
You got /4 concepts.
Practice
(1/5)
1. Why is the order of middleware important in an Express app?
easy
A. Because middleware order determines the database connection speed
B. Because middleware order changes the port number the app listens on
C. Because middleware runs in the order it is added, affecting request flow
D. Because middleware order controls the CSS styles applied to the page
Solution
Step 1: Understand middleware execution
Middleware functions run in the order they are added to the Express app.
Step 2: Connect order to request flow
The order controls how requests are processed and which middleware handles them first.
Final Answer:
Because middleware runs in the order it is added, affecting request flow -> Option C
Quick Check:
Middleware order = request flow control [OK]
Hint: Middleware order controls request handling sequence [OK]
Common Mistakes:
Thinking middleware order affects unrelated app settings
Confusing middleware order with server port configuration
Assuming middleware order changes frontend styles
2. Which of the following is the correct way to add middleware that logs requests before parsing JSON in Express?
easy
A. app.use(express.static('public')); app.use(logger);
B. app.use(express.json()); app.use(logger);
C. app.use(express.urlencoded()); app.use(logger);
D. app.use(logger); app.use(express.json());
Solution
Step 1: Identify middleware order for logging and parsing
Logging middleware should run before JSON parsing to log raw requests.
Step 2: Check the order in options
app.use(logger); app.use(express.json()); adds logger first, then JSON parser, which is correct.
Final Answer:
app.use(logger); app.use(express.json()); -> Option D
Quick Check:
Logger before parser = correct order [OK]
Hint: Put logging middleware before parsers [OK]
Common Mistakes:
Adding parser before logger loses raw request info
Using static middleware instead of logger for logging
A. Because the response is sent before error handler is reached
B. Because express.json() must be last middleware
C. Because error handlers must be added before normal middleware
D. Because error handlers only run on GET requests
Solution
Step 1: Identify middleware order and response flow
The second middleware sends a response immediately, ending the request.
Step 2: Understand error handler placement
Error handlers run only if next(err) is called before response is sent.
Final Answer:
Because the response is sent before error handler is reached -> Option A
Quick Check:
Response sent early blocks error handler [OK]
Hint: Send response last, error handler after all middleware [OK]
Common Mistakes:
Placing error handler before normal middleware
Thinking express.json() must be last
Assuming error handlers run on all requests regardless
5. You want to add middleware that authenticates users, logs requests, parses JSON, and handles errors. Which order is best to ensure correct behavior?
hard
A. Logger, JSON parser, authenticator, error handler
B. Error handler, JSON parser, logger, authenticator
C. Authenticator, error handler, logger, JSON parser
D. JSON parser, authenticator, error handler, logger
Solution
Step 1: Determine middleware roles and order
Logger should run first to record raw requests. JSON parser next to parse body. Authenticator after parsing to check user data. Error handler last to catch errors.