Middleware is a function that runs during the request-response cycle. It can modify the request or response, or end the cycle by sending a response.
Click to reveal answer
beginner
How do you define a custom middleware function in Express?
A custom middleware is a function with three parameters: req, res, and next. It processes the request and calls next() to pass control to the next middleware.
Click to reveal answer
beginner
What does the next() function do in middleware?
next() passes control to the next middleware or route handler. Without calling next(), the request will hang and not proceed.
Click to reveal answer
intermediate
How can middleware modify the request object?
Middleware can add new properties or change existing ones on req. For example, adding req.user after authentication.
Click to reveal answer
intermediate
Where should you place custom middleware in your Express app?
Place middleware before the routes that need it. This ensures the middleware runs first and can affect those routes.
Click to reveal answer
What are the three parameters of an Express middleware function?
Arequest, response, finish
Brequest, response, callback
Creq, res, done
Dreq, res, next
✗ Incorrect
Express middleware functions always receive req, res, and next as parameters.
What happens if you forget to call next() in your middleware?
AThe server will crash immediately.
BThe request will hang and never reach the next middleware or route.
CThe response will automatically be sent.
DThe middleware will skip to the last route.
✗ Incorrect
Without calling next(), Express does not know to continue, so the request hangs.
Which of these is a common use case for custom middleware?
ALogging requests
BRendering HTML templates
CConnecting to a database
DServing static files
✗ Incorrect
Logging requests is a typical middleware task to track incoming traffic.
Where should you place middleware in your Express app?
ABefore the routes that need it
BAfter all routes
COnly inside route handlers
DAnywhere, order does not matter
✗ Incorrect
Middleware must be placed before routes to affect those routes.
How can middleware add data to be used later in the request cycle?
ABy modifying the <code>res</code> headers only
BBy sending a response immediately
CBy adding properties to the <code>req</code> object
DBy calling <code>next()</code> twice
✗ Incorrect
Middleware can add properties to req so later middleware or routes can use that data.
Explain how to create and use a custom middleware function in Express.
Think about the function signature and where it fits in the request flow.
You got /4 concepts.
Describe what happens if you do not call next() inside your middleware.
Consider the flow of control in Express middleware.
You got /3 concepts.
Practice
(1/5)
1. What is the main purpose of custom middleware in a Node.js Express app?
easy
A. To directly send HTML pages to the client
B. To add extra processing steps between receiving a request and sending a response
C. To replace the Express router completely
D. To store data permanently in a database
Solution
Step 1: Understand middleware role
Middleware functions run between receiving a request and sending a response, allowing extra processing.
Step 2: Identify correct purpose
Custom middleware adds steps like logging, authentication, or modifying requests before response.
Final Answer:
To add extra processing steps between receiving a request and sending a response -> Option B
Quick Check:
Middleware = extra processing steps [OK]
Hint: Middleware adds steps between request and response [OK]
2. Which of the following is the correct syntax to define a simple middleware function in Express?
easy
A. app.use = function(req, res, next) { console.log('Hi'); next(); }
B. app.middleware(req, res, next) { console.log('Hi'); next(); }
C. app.use(req, res, next) => { console.log('Hi'); next(); }
D. app.use((req, res, next) => { console.log('Hi'); next(); });
Solution
Step 1: Recall Express middleware syntax
Middleware is added using app.use() with a function taking (req, res, next).
Step 2: Check each option
app.use((req, res, next) => { console.log('Hi'); next(); }); correctly uses app.use with an arrow function and calls next(). Others have syntax errors.
A. Calling next() after sending a response causes error
B. Missing call to next() inside the if block
C. Headers should be accessed as req.header, not req.headers
D. Middleware must always send a response
Solution
Step 1: Analyze middleware flow
If 'x-auth' header is missing, response is sent with 401 status, ending the request.
Step 2: Check next() call after response
Calling next() after sending a response causes Express to continue processing, leading to errors.
Final Answer:
Calling next() after sending a response causes error -> Option A
Quick Check:
Do not call next() after res.send [OK]
Hint: Don't call next() after sending response [OK]
Common Mistakes:
Calling next() after res.send causing double response
Assuming headers property name is wrong
Thinking middleware must always send response
5. You want to create middleware that logs request method and URL only for POST requests. Which code correctly implements this?
hard
A. app.use((req, res, next) => { if (req.method = 'POST') console.log(req.method, req.url); next(); });
B. app.use((req, res, next) => { if (req.method === 'post') console.log(req.method, req.url); next(); });
C. app.use((req, res, next) => { if (req.method === 'POST') console.log(req.method, req.url); next(); });
D. app.use((req, res, next) => { if (req.method === POST) console.log(req.method, req.url); next(); });
Solution
Step 1: Check condition for POST method
Correct comparison uses '===' and string 'POST' in uppercase.
Step 2: Verify logging and next call
app.use((req, res, next) => { if (req.method === 'POST') console.log(req.method, req.url); next(); }); logs method and URL only if method is POST, then calls next() to continue.
Final Answer:
app.use((req, res, next) => { if (req.method === 'POST') console.log(req.method, req.url); next(); }); -> Option C
Quick Check:
Use strict equality and uppercase 'POST' [OK]
Hint: Use === 'POST' to check method string correctly [OK]
Common Mistakes:
Using single = instead of === for comparison
Using lowercase 'post' instead of uppercase
Using undefined POST variable without quotes
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.