0
0
Expressframework~20 mins

Middleware ordering and its importance in Express - 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 when middleware order is changed?

Consider this Express app code snippet. What will be the response when a GET request is made to /test?

const express = require('express');
const app = express();

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

app.get('/test', (req, res, next) => {
  res.send('Hello from /test');
  next();
});

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

app.listen(3000);

Now, if we move the fallback middleware app.use((req, res) => {...}) above the app.get('/test', ...) route, what will happen?

Express
const express = require('express');
const app = express();

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

// Fallback middleware moved here
app.use((req, res) => {
  res.send('Fallback middleware');
});

app.get('/test', (req, res, next) => {
  res.send('Hello from /test');
  next();
});

app.listen(3000);
AThe response will be empty because next() is called after res.send() in the route.
BThe response will be 'Hello from /test' because route handlers always run before middleware.
CThe server will crash with an error because middleware order is invalid.
DThe response will be 'Fallback middleware' for GET /test because fallback middleware runs before the route handler.
Attempts:
2 left
💡 Hint

Think about how Express processes middleware and routes in the order they are added.

lifecycle
intermediate
2:00remaining
Which middleware runs first in this Express app?

Given the following Express app setup, which middleware function will run first when a request is made to /api/data?

const express = require('express');
const app = express();

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

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

app.get('/api/data', (req, res) => {
  res.send('Data response');
});

app.listen(3000);
A'General middleware' runs first because it is registered before the API middleware.
B'API middleware' runs first because it matches the /api path prefix.
CBoth middlewares run simultaneously.
DOnly the route handler runs; middleware is skipped.
Attempts:
2 left
💡 Hint

Remember that middleware with path prefixes only run if the request path matches.

🔧 Debug
advanced
2:00remaining
Why does this Express app hang without responding?

Look at this Express app code. Why does it hang and never send a response?

const express = require('express');
const app = express();

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

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

app.listen(3000);
AMiddleware 1 does not call next(), so the request never reaches the route handler.
BThe app.listen() call is missing a callback, causing the hang.
CExpress requires res.end() to be called explicitly in middleware.
DThe route handler is missing a next() call, causing the hang.
Attempts:
2 left
💡 Hint

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

📝 Syntax
advanced
2:00remaining
What error does this middleware code produce?

Examine this Express middleware code snippet. What error will it cause?

app.use((req, res, next) => {
  if(req.method === 'GET')
    next();
  else
    res.send('Only GET allowed');
    next();
});
AError: Cannot set headers after they are sent to the client
BSyntaxError: Unexpected token else
CNo error; middleware works correctly
DTypeError: next is not a function
Attempts:
2 left
💡 Hint

Look carefully at the indentation and flow of next() calls.

🧠 Conceptual
expert
2:00remaining
Why is middleware ordering critical in Express apps?

Which of the following best explains why the order of middleware matters in Express?

AMiddleware order only matters if you use error-handling middleware.
BMiddleware order only affects performance but not the correctness of responses.
CMiddleware order determines the sequence of request processing; incorrect order can block routes or cause unexpected responses.
DMiddleware order is irrelevant because Express runs all middleware in parallel.
Attempts:
2 left
💡 Hint

Think about how Express passes requests through middleware and routes.