0
0
Node.jsframework~20 mins

Middleware ordering matters in Node.js - Practice Problems & Coding Challenges

Choose your learning style9 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.