Bird
Raised Fist0
Expressframework~20 mins

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

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

Practice

(1/5)
1. Why is middleware considered the core concept in Express?
easy
A. Because it allows adding reusable functions to process requests and responses
B. Because it automatically generates HTML pages
C. Because it replaces the need for routing in Express
D. Because it stores data permanently in the database

Solution

  1. Step 1: Understand middleware role

    Middleware functions run during request processing to handle tasks like logging, authentication, or parsing.
  2. Step 2: Identify middleware's core purpose

    It allows adding reusable steps that can modify requests or responses before final handling.
  3. Final Answer:

    Because it allows adding reusable functions to process requests and responses -> Option A
  4. Quick Check:

    Middleware = reusable request handlers [OK]
Hint: Middleware = reusable steps for requests [OK]
Common Mistakes:
  • Thinking middleware generates HTML automatically
  • Confusing middleware with database storage
  • Believing middleware replaces routing
2. Which of the following is the correct syntax to use middleware in Express?
easy
A. app.route(function(req, res, next) { next(); });
B. app.middleware(function(req, res) { res.send('Hi'); });
C. app.use(function(req, res, next) { next(); });
D. app.handle(function(req, res) { next(); });

Solution

  1. Step 1: Recall Express middleware syntax

    Middleware is added using app.use() with a function that takes req, res, next.
  2. Step 2: Match syntax to options

    Only app.use(function(req, res, next) { next(); }); uses app.use and the correct function signature.
  3. Final Answer:

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

    Middleware added with app.use() [OK]
Hint: Middleware uses app.use() with (req, res, next) [OK]
Common Mistakes:
  • Using app.middleware instead of app.use
  • Omitting the next parameter in middleware
  • Confusing app.route with middleware
3. What will be the output when this middleware is used?
app.use((req, res, next) => {
  console.log('Middleware 1');
  next();
});
app.use((req, res) => {
  res.send('Hello from Express');
});
medium
A. The server logs 'Middleware 1' and responds with 'Hello from Express'
B. The server responds with 'Middleware 1' text
C. The server throws an error because next() is missing
D. Nothing happens because middleware is not called

Solution

  1. Step 1: Analyze first middleware

    It logs 'Middleware 1' and calls next(), passing control to next middleware.
  2. Step 2: Analyze second middleware

    It sends the response 'Hello from Express', ending the request.
  3. Final Answer:

    The server logs 'Middleware 1' and responds with 'Hello from Express' -> Option A
  4. Quick Check:

    Middleware logs then sends response [OK]
Hint: Middleware logs then calls next() to continue [OK]
Common Mistakes:
  • Thinking next() is missing causing error
  • Expecting middleware to send response twice
  • Assuming middleware does not run in order
4. Identify the error in this middleware code:
app.use((req, res, next) => {
  res.send('Hello');
  next();
});
medium
A. Missing next parameter in function
B. Middleware must return a value
C. Middleware should not use res.send()
D. Calling next() after res.send() causes an error

Solution

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

    res.send() ends the response and sends data to client.
  2. Step 2: Analyze calling next() after res.send()

    Calling next() after response is sent causes Express to try to continue processing, leading to errors.
  3. Final Answer:

    Calling next() after res.send() causes an error -> Option D
  4. Quick Check:

    Do not call next() after sending response [OK]
Hint: Don't call next() after res.send() [OK]
Common Mistakes:
  • Thinking next() is missing
  • Believing middleware must return a value
  • Assuming res.send() can be called multiple times
5. You want to create middleware that logs request method and URL, then passes control. Which code correctly implements this?
hard
A. app.use((req, res, next) => { next(); console.log(req.method, req.url); });
B. app.use((req, res, next) => { console.log(req.method, req.url); next(); });
C. app.use((req, res, next) => { res.send(req.method + req.url); next(); });
D. app.use((req, res) => { console.log(req.method, req.url); });

Solution

  1. Step 1: Check middleware signature and behavior

    Middleware must accept (req, res, next) and call next() to continue.
  2. Step 2: Verify logging and next call order

    Logging before calling next() ensures the log happens before moving on.
  3. Step 3: Evaluate options

    app.use((req, res, next) => { console.log(req.method, req.url); next(); }); logs method and URL, then calls next() correctly. app.use((req, res) => { console.log(req.method, req.url); }); misses next(), app.use((req, res, next) => { res.send(req.method + req.url); next(); }); sends response prematurely, app.use((req, res, next) => { next(); console.log(req.method, req.url); }); logs after next() which may be too late.
  4. Final Answer:

    app.use((req, res, next) => { console.log(req.method, req.url); next(); }); -> Option B
  5. Quick Check:

    Log then next() in middleware [OK]
Hint: Log first, then call next() in middleware [OK]
Common Mistakes:
  • Omitting next() parameter
  • Sending response inside logging middleware
  • Calling next() before logging
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