Middleware is a function that runs during the request-response cycle. It can modify the request or response, or end the request.
Click to reveal answer
beginner
How can you run middleware only for certain routes in Express?
You can attach middleware only to specific routes by passing it as an argument before the route handler, so it runs conditionally.
Click to reveal answer
intermediate
How do you conditionally execute middleware based on a request property?
Inside the middleware, check the request property (like req.path or req.method). Call next() only if the condition is met; otherwise, skip or respond.
Click to reveal answer
beginner
What does calling next() do in Express middleware?
Calling next() passes control to the next middleware or route handler in the stack.
Click to reveal answer
intermediate
Give an example of conditional middleware execution in Express.
Example: Use middleware only if req.query.admin is 'true'. Inside middleware, check if req.query.admin === 'true', then call next(); else respond with 403.
Click to reveal answer
What is the purpose of middleware in Express?
ATo store data permanently
BTo style HTML pages
CTo create database connections
DTo handle requests and responses during the request cycle
✗ Incorrect
Middleware functions run during the request-response cycle to process requests or responses.
How do you run middleware only for a specific route in Express?
AAttach middleware as an argument before the route handler
BWrite middleware inside the route handler
CUse middleware globally for all routes
DMiddleware cannot be route-specific
✗ Incorrect
Passing middleware before the route handler runs it only for that route.
What happens if you don't call next() inside middleware?
AThe request will hang and not proceed
BThe next middleware runs automatically
CThe server restarts
DThe response is sent automatically
✗ Incorrect
Without calling next(), Express does not continue to the next middleware or route.
Which property can you check to conditionally run middleware?
Aconsole.log
Breq.method
Cres.statusCode
Dprocess.env
✗ Incorrect
req.method holds the HTTP method and can be used to conditionally run middleware.
How can you skip middleware execution for some requests?
AMiddleware always runs for all requests
BRemove middleware from the app
CCheck condition inside middleware and call next() only if true
DUse a different server
✗ Incorrect
Inside middleware, you can decide to call next() or respond early based on conditions.
Explain how to run middleware conditionally in Express based on the request path.
Think about how to decide inside middleware whether to continue or stop.
You got /4 concepts.
Describe the role of next() in Express middleware and what happens if it is not called.
Consider the flow of request handling in Express.
You got /3 concepts.
Practice
(1/5)
1. What is the main purpose of conditional middleware in Express?
easy
A. To run middleware only when certain conditions are met
B. To run all middleware for every request without exception
C. To replace route handlers completely
D. To stop the server from accepting requests
Solution
Step 1: Understand middleware role
Middleware functions process requests and can modify them or respond.
Step 2: Identify conditional middleware purpose
Conditional middleware runs only when a specific condition is true, saving resources and controlling flow.
Final Answer:
To run middleware only when certain conditions are met -> Option A
Quick Check:
Conditional middleware = run only if condition true [OK]
Hint: Think: middleware runs only if condition matches [OK]
Common Mistakes:
Assuming middleware always runs for every request
Confusing middleware with route handlers
Thinking middleware stops server requests
2. Which of the following is the correct way to conditionally execute middleware based on the request path?
easy
A. app.use((req, res, next) => { if (req.path === '/admin') res.send('OK'); });
B. app.use((req, res, next) => { if (req.method === 'POST') res.send('Allowed'); else next(); });
C. app.use((req, res, next) => { if (req.path = '/admin') next(); else next(); });
D. app.use((req, res, next) => { if (req.path === '/admin') next(); else res.send('No access'); });
Solution
Step 1: Check condition syntax
app.use((req, res, next) => { if (req.path === '/admin') next(); else res.send('No access'); }); correctly uses '===' to compare req.path to '/admin'.
Step 2: Verify middleware flow
app.use((req, res, next) => { if (req.path === '/admin') next(); else res.send('No access'); }); calls next() only if path matches; otherwise, it sends a response, stopping further middleware.
Final Answer:
app.use((req, res, next) => { if (req.path === '/admin') next(); else res.send('No access'); }); -> Option D
Quick Check:
Correct condition and flow = app.use((req, res, next) => { if (req.path === '/admin') next(); else res.send('No access'); }); [OK]
Hint: Use '===' for comparison and call next() properly [OK]
Common Mistakes:
Using single '=' instead of '===' for comparison
Not calling next() to continue middleware chain
Sending response without ending middleware properly
If header exists, next() is called but code continues to send 403 response anyway.
Step 2: Identify missing else
Without else, response is sent after next(), causing errors or double responses.
Final Answer:
Missing else block causes response sent twice -> Option C
Quick Check:
Always use else or return after next() [OK]
Hint: Use else or return after next() to avoid double response [OK]
Common Mistakes:
Not using else or return after next()
Assuming next() stops code execution
Ignoring middleware flow after next()
5. You want to create middleware that only runs for POST requests to '/submit' and logs 'Processing submission'. Which code correctly implements this conditional middleware?
hard
A. app.use((req, res, next) => { if (req.path === '/submit' && req.method === 'POST') { console.log('Processing submission'); } next(); });
B. app.use('/submit', (req, res, next) => { if (req.method === 'POST') { console.log('Processing submission'); } next(); });