0
0
Node.jsframework~20 mins

Why middleware is fundamental in Node.js - Challenge Your Understanding

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!
🧠 Conceptual
intermediate
2:00remaining
What is the primary role of middleware in Node.js web frameworks?

Middleware functions are used in Node.js web frameworks like Express. What is their main purpose?

ATo compile JavaScript code into machine code for faster execution
BTo directly send HTML pages to the client without any processing
CTo handle requests and responses by processing data before reaching the final route handler
DTo replace the need for a database in the application
Attempts:
2 left
💡 Hint

Think about how middleware acts between the client request and the final response.

component_behavior
intermediate
2:00remaining
How does middleware affect the flow of request handling?

Consider this Express middleware chain:

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 '/'?

ASecond\nFirst
BFirst\nSecond
CDone
DNo output because next() is missing
Attempts:
2 left
💡 Hint

Middleware functions run in the order they are added and call next() to continue.

📝 Syntax
advanced
2:00remaining
Identify the error in this middleware function

Look at this middleware code snippet:

app.use((req, res, next) => {
  if (!req.user) {
    res.status(401).send('Unauthorized');
  }
  next();
});

What problem will this cause when a request has no user?

AIt will send 'Unauthorized' but then call next(), causing headers to be sent twice error
BIt will throw a syntax error because next() is missing parentheses
CIt will not send any response and hang forever
DIt will correctly block unauthorized requests without errors
Attempts:
2 left
💡 Hint

Think about what happens after sending a response in middleware.

state_output
advanced
2:00remaining
What is the final response body after this middleware chain?

Given this Express setup:

app.use((req, res, next) => {
  req.customData = 'Hello';
  next();
});
app.use((req, res, next) => {
  req.customData += ' World';
  next();
});
app.get('/', (req, res) => {
  res.send(req.customData);
});

What will the client receive when requesting '/'?

AAn error because req.customData is undefined
B"Hello"
C"World"
D"Hello World"
Attempts:
2 left
💡 Hint

Middleware can add or change properties on the request object.

🔧 Debug
expert
2:00remaining
Why does this middleware never end the request?

Examine this middleware:

app.use((req, res, next) => {
  if (req.path === '/stop') {
    return res.send('Stopped');
  } else {
    next();
  }
});

Requests to '/stop' hang and never complete. Why?

ABecause the middleware is missing a return statement after res.send() to stop execution
BBecause the middleware does not call next() for '/stop' paths, so Express waits forever
CBecause res.send() is asynchronous and needs await keyword
DBecause res.send() is called but next() is not stopped, causing the request to hang
Attempts:
2 left
💡 Hint

Think about what happens after res.send() inside an if block.