Bird
Raised Fist0
Expressframework~20 mins

Middleware execution flow (req, res, next) in Express - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Middleware Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate
2:00remaining
What is the output of this Express middleware sequence?
Consider this Express app code with three middleware functions. What will be sent to the client when a GET request is made to '/'?
Express
const express = require('express');
const app = express();

app.use((req, res, next) => {
  req.message = 'Hello';
  next();
});

app.use((req, res, next) => {
  req.message += ' World';
  next();
});

app.get('/', (req, res) => {
  res.send(req.message);
});
AError: req.message is undefined
B"Hello"
C"World"
D"Hello World"
Attempts:
2 left
💡 Hint
Think about how middleware modifies the request object and calls next() to pass control.
❓ lifecycle
intermediate
2:00remaining
Which middleware runs last before the response is sent?
Given this Express middleware setup, which middleware function executes immediately before the response is sent to the client?
Express
const express = require('express');
const app = express();

app.use((req, res, next) => {
  console.log('Middleware 1');
  next();
});

app.use((req, res, next) => {
  console.log('Middleware 2');
  next();
});

app.get('/', (req, res) => {
  console.log('Route handler');
  res.send('Done');
});
ARoute handler
BMiddleware 2
CNo middleware runs before response
DMiddleware 1
Attempts:
2 left
💡 Hint
Remember the order middleware and route handlers run in Express.
🔧 Debug
advanced
2:00remaining
Why does this middleware never call the next middleware?
Examine this middleware. Why does the next middleware not run after this one?
Express
app.use((req, res, next) => {
  if (!req.user) {
    res.status(401).send('Unauthorized');
  } else {
    next();
  }
});

app.use((req, res, next) => {
  console.log('Second middleware');
  next();
});
ABecause res.send() ends the response and next() is not called when !req.user
BBecause next() is called twice causing an error
CBecause the middleware is not registered properly
DBecause req.user is always truthy
Attempts:
2 left
💡 Hint
Think about what happens when res.send() is called without next().
📝 Syntax
advanced
2:00remaining
What error does this middleware code produce?
Identify the error caused by this middleware function code.
Express
app.use((req, res, next) => {
  console.log('Start');
  next();
  console.log('End');
});
ANo error, middleware runs fine
BReferenceError: next is not defined
CSyntaxError: Unexpected identifier
DTypeError: next is not a function
Attempts:
2 left
💡 Hint
Look carefully at how next is used.
❓ state_output
expert
3:00remaining
What is the final value of req.count after this middleware chain?
Given this middleware chain, what is the value of req.count when the response is sent?
Express
app.use((req, res, next) => {
  req.count = 1;
  next();
});

app.use((req, res, next) => {
  req.count += 2;
  next();
});

app.use((req, res, next) => {
  req.count *= 3;
  next();
});

app.get('/', (req, res) => {
  res.send(req.count.toString());
});
A"7"
B"9"
C"3"
D"6"
Attempts:
2 left
💡 Hint
Calculate step-by-step how req.count changes in each middleware.

Practice

(1/5)
1. What does the next() function do in Express middleware?
easy
A. It passes control to the next middleware or route handler.
B. It sends the response back to the client immediately.
C. It stops the middleware chain and ends the request.
D. It modifies the request object permanently.

Solution

  1. Step 1: Understand middleware flow

    Middleware functions receive req, res, and next. Calling next() tells Express to continue to the next middleware or route.
  2. Step 2: Identify the role of next()

    Calling next() does not send a response or stop the chain; it moves control forward.
  3. Final Answer:

    It passes control to the next middleware or route handler. -> Option A
  4. Quick Check:

    next() = pass control [OK]
Hint: Remember: next() moves to the next step in middleware [OK]
Common Mistakes:
  • Thinking next() sends a response
  • Assuming next() stops middleware chain
  • Confusing next() with modifying req or res
2. Which of the following is the correct way to define middleware in Express?
easy
A. app.use((req, res, next) => { next(); });
B. app.use((req, res) => { next(); });
C. app.use((req, res, next) => { res.next(); });
D. app.use((req, res, next) => { next; });

Solution

  1. Step 1: Check middleware function signature

    Express middleware must have three parameters: req, res, and next.
  2. Step 2: Verify correct usage of next()

    Calling next() as a function is required to pass control. app.use((req, res, next) => { next(); }); correctly uses next() with all parameters.
  3. Final Answer:

    app.use((req, res, next) => { next(); }); -> Option A
  4. Quick Check:

    Middleware signature + next() call = app.use((req, res, next) => { next(); }); [OK]
Hint: Middleware needs (req, res, next) and call next() as a function [OK]
Common Mistakes:
  • Omitting the next parameter
  • Calling next without parentheses
  • Using res.next() which does not exist
3. Consider this Express middleware chain:
app.use((req, res, next) => { console.log('A'); next(); });
app.use((req, res, next) => { console.log('B'); next(); });
app.use((req, res) => { res.send('Done'); });

What will be printed in the console when a request is made?
medium
A. A
B. A B
C. B A
D. No output

Solution

  1. Step 1: Trace middleware execution order

    The first middleware logs 'A' and calls next(), so control moves to the second middleware.
  2. Step 2: Continue to second middleware

    The second middleware logs 'B' and calls next(), moving to the third middleware which sends the response.
  3. Final Answer:

    A B -> Option B
  4. Quick Check:

    Middleware logs in order = A B [OK]
Hint: Middleware runs in order; next() moves forward [OK]
Common Mistakes:
  • Assuming logs appear after response
  • Thinking next() skips middleware
  • Confusing order of middleware calls
4. What is the problem with this middleware code?
app.use((req, res, next) => {
  console.log('Start');
  // missing next() call
});
app.use((req, res) => {
  res.send('Hello');
});
medium
A. The second middleware is missing next parameter.
B. console.log() causes an error in middleware.
C. res.send() cannot be called in middleware.
D. The first middleware never calls next(), so request hangs.

Solution

  1. Step 1: Check middleware flow control

    The first middleware logs 'Start' but does not call next() or send a response, so Express waits indefinitely.
  2. Step 2: Understand consequence of missing next()

    Without calling next(), the second middleware never runs, and the client gets no response.
  3. Final Answer:

    The first middleware never calls next(), so request hangs. -> Option D
  4. Quick Check:

    Missing next() causes hang [OK]
Hint: Always call next() or send response to avoid hanging [OK]
Common Mistakes:
  • Ignoring missing next() causes no response
  • Thinking console.log() breaks middleware
  • Assuming res.send() is invalid in middleware
5. You want to create middleware that logs request method and URL, then passes control only if the method is GET. Otherwise, it should send a 405 status. Which code correctly implements this?
hard
A. app.use((req, res, next) => { console.log(req.method, req.url); next(); res.status(405).send('Method Not Allowed'); });
B. app.use((req, res, next) => { console.log(req.method, req.url); if (req.method === 'GET') res.send('OK'); else next(); });
C. app.use((req, res, next) => { console.log(req.method, req.url); if (req.method === 'GET') next(); else res.status(405).send('Method Not Allowed'); });
D. app.use((req, res) => { console.log(req.method, req.url); if (req.method !== 'GET') next(); else res.status(405).send('Method Not Allowed'); });

Solution

  1. Step 1: Log method and URL

    The middleware logs req.method and req.url correctly in all options.
  2. Step 2: Control flow based on method

    app.use((req, res, next) => { console.log(req.method, req.url); if (req.method === 'GET') next(); else res.status(405).send('Method Not Allowed'); }); calls next() only if method is GET, else sends 405 status. This matches the requirement.
  3. Step 3: Identify incorrect options

    app.use((req, res, next) => { console.log(req.method, req.url); if (req.method === 'GET') res.send('OK'); else next(); }); sends 'OK' on GET instead of passing control. app.use((req, res, next) => { console.log(req.method, req.url); next(); res.status(405).send('Method Not Allowed'); }); calls next() then tries to send response (invalid). app.use((req, res) => { console.log(req.method, req.url); if (req.method !== 'GET') next(); else res.status(405).send('Method Not Allowed'); }); lacks next parameter and reverses logic.
  4. Final Answer:

    app.use((req, res, next) => { console.log(req.method, req.url); if (req.method === 'GET') next(); else res.status(405).send('Method Not Allowed'); }); -> Option C
  5. Quick Check:

    next() only on GET, else 405 = app.use((req, res, next) => { console.log(req.method, req.url); if (req.method === 'GET') next(); else res.status(405).send('Method Not Allowed'); }); [OK]
Hint: Call next() only for GET; send 405 otherwise [OK]
Common Mistakes:
  • Calling next() then sending response
  • Swapping condition logic
  • Missing next parameter in middleware
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard