0
0
Expressframework~20 mins

Why middleware is Express's core concept - Challenge Your Understanding

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Express Middleware Master
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 Express?

In Express, middleware functions are essential. What is their main purpose?

AThey process requests and responses, allowing code to run between receiving a request and sending a response.
BThey replace the need for routing in Express applications.
CThey automatically generate HTML pages without any code.
DThey store data permanently on the server like a database.
Attempts:
2 left
💡 Hint

Think about what happens after a request arrives but before the response is sent.

component_behavior
intermediate
2:00remaining
How does middleware affect request handling order in Express?

Consider an Express app with multiple middleware functions. How does Express decide the order in which middleware runs?

AMiddleware runs in the order it is added to the app, from top to bottom.
BMiddleware runs randomly depending on server load.
CMiddleware runs only if the previous middleware returns true.
DMiddleware runs in reverse order of how it was added.
Attempts:
2 left
💡 Hint

Think about how you write middleware in your code file.

📝 Syntax
advanced
2:00remaining
What is the correct syntax to define a middleware function in Express?

Which of the following code snippets correctly defines a middleware function that logs the request method and URL?

Express
app.use( /* your middleware here */ );
A(req, res, next) => { console.log(req.method, req.url); }
Bfunction(req, res) { console.log(req.method, req.url); }
Cfunction(req, res, next) { console.log(req.method, req.url); next(); }
D(req, res) => { console.log(req.method, req.url); next(); }
Attempts:
2 left
💡 Hint

Middleware needs to call next() to pass control to the next function.

🔧 Debug
advanced
2:00remaining
Why does this middleware cause the request to hang?

Look at this middleware code:

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

What is the reason the client never gets a response?

ABecause <code>console.log</code> blocks the event loop indefinitely.
BBecause <code>next()</code> is not called, Express does not continue to the next middleware or route handler.
CBecause the middleware returns a value instead of calling <code>next()</code>.
DBecause the middleware should send a response but does not.
Attempts:
2 left
💡 Hint

Think about how Express moves from one middleware to the next.

state_output
expert
2:00remaining
What is the output when multiple middleware modify the request object?

Given this Express app code:

app.use((req, res, next) => {
  req.customValue = 1;
  next();
});
app.use((req, res, next) => {
  req.customValue += 2;
  next();
});
app.get('/', (req, res) => {
  res.send(`Value is ${req.customValue}`);
});

What will the client see when requesting /?

AAn error because <code>customValue</code> is not defined
B"Value is 1"
C"Value is undefined"
D"Value is 3"
Attempts:
2 left
💡 Hint

Consider how middleware can add or change properties on the req object.