Bird
Raised Fist0
Node.jsframework~20 mins

Middleware ordering matters in Node.js - 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 middleware setup?

Consider this Express.js app with two middleware functions. What will be the console output and response?

Node.js
const express = require('express');
const app = express();

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

app.use((req, res, next) => {
  console.log('Second middleware');
  res.send('Hello from second');
});

app.listen(3000);
AConsole logs: 'First middleware' then 'Second middleware', response: 'Hello from second'
BConsole logs: 'Second middleware' then 'First middleware', response: 'Hello from second'
CConsole logs: 'First middleware' only, response: no response (timeout)
DConsole logs: 'Second middleware' only, response: 'Hello from first'
Attempts:
2 left
💡 Hint

Middleware runs in the order they are added. The first calls next(), so the second runs next.

❓ state_output
intermediate
2:00remaining
What is the value of req.processed after middleware chain?

Given this Express.js middleware chain, what is the final value of req.processed when the response is sent?

Node.js
const express = require('express');
const app = express();

app.use((req, res, next) => {
  req.processed = 1;
  next();
});

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

app.use((req, res) => {
  res.send(`Processed: ${req.processed}`);
});

app.listen(3000);
AProcessed: undefined
BProcessed: 2
CProcessed: 3
DProcessed: 1
Attempts:
2 left
💡 Hint

Each middleware modifies req.processed and calls next() to continue.

🔧 Debug
advanced
2:00remaining
Why does the response never send in this middleware setup?

Look at this Express.js code. Why does the client never receive a response?

Node.js
const express = require('express');
const app = express();

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

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

app.listen(3000);
AThe first middleware throws an error, stopping the chain.
BThe first middleware never calls next(), so the second middleware never runs, causing no response.
CThe second middleware is never registered due to syntax error.
DThe server crashes because res.send is called twice.
Attempts:
2 left
💡 Hint

Middleware must call next() to continue to the next middleware.

📝 Syntax
advanced
2:00remaining
Which middleware setup causes a syntax error?

Which of these Express.js middleware declarations will cause a syntax error?

Aapp.use((req, res, next) => { console.log('A'); next(); });
Bapp.use(function(req, res, next) { console.log('C'); next(); });
Capp.use((req, res, next) => { console.log('D'); next(); });
Dapp.use((req, res next) => { console.log('B'); next(); });
Attempts:
2 left
💡 Hint

Check the function parameter list syntax carefully.

🧠 Conceptual
expert
3:00remaining
Which middleware order ensures error handling works correctly?

You have three middleware: logger, errorHandler, and router. Which order ensures that errors from router are caught by errorHandler?

Aapp.use(logger); app.use(router); app.use(errorHandler);
Bapp.use(errorHandler); app.use(logger); app.use(router);
Capp.use(router); app.use(logger); app.use(errorHandler);
Dapp.use(router); app.use(errorHandler); app.use(logger);
Attempts:
2 left
💡 Hint

Error handling middleware must come after the middleware that might cause errors.

Practice

(1/5)
1. In a Node.js Express app, why does the order of middleware matter?
easy
A. Because middleware order controls the file system permissions
B. Because middleware order changes the port the server listens on
C. Because middleware order determines the database connection string
D. Because middleware runs in the order it is added, affecting request handling

Solution

  1. Step 1: Understand middleware execution order

    Middleware functions in Express run in the sequence they are added to the app. This order controls how requests are processed.
  2. Step 2: Recognize impact on request handling

    If middleware is out of order, some requests might not be handled correctly or responses might be sent too early.
  3. Final Answer:

    Because middleware runs in the order it is added, affecting request handling -> Option D
  4. Quick Check:

    Middleware order affects request flow = D [OK]
Hint: Middleware runs top to bottom in code order [OK]
Common Mistakes:
  • Thinking middleware order affects server port
  • Confusing middleware order with database config
  • Assuming middleware order controls file permissions
2. Which of the following is the correct way to add middleware in Express to ensure it runs before route handlers?
easy
A. app.use(middlewareFunction); app.get('/route', handler);
B. app.get('/route', handler); app.use(middlewareFunction);
C. app.listen(3000); app.use(middlewareFunction);
D. app.get('/route', middlewareFunction, handler); app.use();

Solution

  1. Step 1: Identify middleware placement

    Middleware should be added before route handlers to process requests first.
  2. Step 2: Check code order

    app.use(middlewareFunction); app.get('/route', handler); adds middleware with app.use before defining the route, ensuring middleware runs first.
  3. Final Answer:

    app.use(middlewareFunction); app.get('/route', handler); -> Option A
  4. Quick Check:

    Middleware before routes = C [OK]
Hint: Add middleware before routes to run first [OK]
Common Mistakes:
  • Adding middleware after routes so it never runs
  • Calling app.listen before middleware
  • Incorrect middleware syntax in routes
3. Consider this Express code snippet:
app.use((req, res, next) => { console.log('First'); next(); });
app.use((req, res, next) => { console.log('Second'); next(); });
app.get('/', (req, res) => { res.send('Done'); });

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

  1. Step 1: Trace middleware execution order

    The first middleware logs 'First' then calls next(), so the second middleware runs next.
  2. Step 2: Trace second middleware and route handler

    The second middleware logs 'Second' then calls next(), leading to the route handler sending 'Done'.
  3. Final Answer:

    First\nSecond -> Option A
  4. Quick Check:

    Middleware logs in order added = B [OK]
Hint: Middleware logs appear in code order [OK]
Common Mistakes:
  • Assuming logs appear reversed
  • Expecting 'Done' in console instead of response
  • Thinking middleware skips if next() is called
4. You have this Express code:
app.use((req, res, next) => { res.send('Hello'); next(); });
app.use((req, res, next) => { console.log('Middleware 2'); next(); });

What is the problem with this middleware order?
medium
A. Middleware 1 does not call next(), so Middleware 2 runs twice
B. Middleware 2 will never run because next() is missing
C. Calling next() after res.send() causes an error or unexpected behavior
D. There is no problem; this order is correct

Solution

  1. Step 1: Understand res.send() effect

    Calling res.send() sends the response and ends the request cycle.
  2. Step 2: Analyze calling next() after res.send()

    Calling next() after res.send() can cause errors or unexpected behavior because the response is already sent.
  3. Final Answer:

    Calling next() after res.send() causes an error or unexpected behavior -> Option C
  4. Quick Check:

    Don't call next() after res.send() = A [OK]
Hint: Never call next() after sending response [OK]
Common Mistakes:
  • Thinking next() is optional after res.send()
  • Assuming middleware 2 runs normally
  • Ignoring that response ends request
5. You want to add a logging middleware that logs every request, an authentication middleware that blocks unauthorized users, and a route handler. Which order ensures unauthorized requests are blocked before routes run, but all requests are logged?
hard
A. Authentication middleware, Logging middleware, Route handler
B. Logging middleware, Authentication middleware, Route handler
C. Route handler, Logging middleware, Authentication middleware
D. Route handler, Authentication middleware, Logging middleware

Solution

  1. Step 1: Determine middleware purpose order

    Logging should happen for all requests first, so it must be first.
  2. Step 2: Place authentication before route handler

    Authentication must run before routes to block unauthorized access.
  3. Step 3: Confirm route handler last

    Route handler runs last after middleware passes.
  4. Final Answer:

    Logging middleware, Authentication middleware, Route handler -> Option B
  5. Quick Check:

    Log first, then auth, then route = A [OK]
Hint: Log first, authenticate second, handle route last [OK]
Common Mistakes:
  • Putting authentication before logging misses some logs
  • Running route before middleware breaks flow
  • Assuming order does not affect blocking unauthorized users
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