Bird
Raised Fist0
Expressframework~20 mins

Application-level middleware in Express - Practice Problems & Coding Challenges

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!
❓ component_behavior
intermediate
2:00remaining
What is the output of this Express middleware chain?
Consider the following Express app code. What will the client receive when making a GET request to '/'?
Express
const express = require('express');
const app = express();

app.use((req, res, next) => {
  req.customValue = 'Hello';
  next();
});

app.use((req, res, next) => {
  req.customValue += ' World';
  next();
});

app.get('/', (req, res) => {
  res.send(req.customValue);
});
A"Hello"
B"Hello World"
C"World"
DError: req.customValue is undefined
Attempts:
2 left
💡 Hint
Think about how middleware modifies the request object before the route handler.
❓ lifecycle
intermediate
2:00remaining
In what order are these middlewares executed?
Given this Express app setup, what is the order of console logs when a GET request to '/' is made?
Express
const express = require('express');
const app = express();

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

app.get('/', (req, res, next) => {
  console.log('Route handler');
  next();
});

app.use((req, res) => {
  console.log('Last middleware');
  res.send('Done');
});
AFirst middleware -> Route handler -> Last middleware
BFirst middleware -> Last middleware -> Route handler
CRoute handler -> First middleware -> Last middleware
DLast middleware -> First middleware -> Route handler
Attempts:
2 left
💡 Hint
Remember that app.use middlewares run in the order they are added.
🔧 Debug
advanced
2:00remaining
Why does this cause a 'headers already sent' error?
Examine this Express app. Why does the server throw an error when making a GET request to '/stop'?
Express
const express = require('express');
const app = express();

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

app.get('/stop', (req, res) => {
  res.send('This will never run');
});
ABecause next() is called after res.send(), causing headers to be sent twice
BBecause res.send() is missing inside the if block
CBecause the middleware does not call next() when path is '/stop'
DBecause the route handler is missing for '/stop'
Attempts:
2 left
💡 Hint
Think about what happens when next() is called after sending a response.
📝 Syntax
advanced
2:00remaining
Which middleware definition is syntactically correct?
Identify the correct syntax for an Express application-level middleware function.
Aapp.use((req, res, next) => { next; })
Bapp.use((req, res) => { next(); })
Capp.use((req, res, next) => { res.send('Hi') next(); })
Dapp.use((req, res, next) => { res.send('Hi'); })
Attempts:
2 left
💡 Hint
Middleware functions must have three parameters and call next() properly if not ending response.
❓ state_output
expert
3:00remaining
What is the final value of req.processed after all middleware run?
Given this Express app, what is the value of req.processed when the client requests '/'?
Express
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.get('/', (req, res) => {
  req.processed *= 3;
  res.send(req.processed.toString());
});
A"3"
B"1"
C"9"
D"6"
Attempts:
2 left
💡 Hint
Track the changes to req.processed step by step through middleware and route.

Practice

(1/5)
1. What is the main purpose of application-level middleware in Express?
easy
A. To run code for every request or specific routes before the final handler
B. To directly send HTML files to the client
C. To connect Express with a database
D. To create new HTTP servers

Solution

  1. Step 1: Understand middleware role

    Application-level middleware runs before route handlers to process requests.
  2. Step 2: Identify middleware usage

    It can modify requests, responses, or decide to stop the request cycle early.
  3. Final Answer:

    To run code for every request or specific routes before the final handler -> Option A
  4. Quick Check:

    Middleware runs before handlers [OK]
Hint: Middleware runs before routes to process requests [OK]
Common Mistakes:
  • Confusing middleware with route handlers
  • Thinking middleware creates servers
  • Assuming middleware connects databases directly
2. Which of the following is the correct syntax to add application-level middleware in Express?
easy
A. app.addMiddleware(function(req, res) { next(); });
B. app.middleware((req, res) => { console.log('Middleware'); });
C. app.use((req, res, next) => { console.log('Middleware'); next(); });
D. app.routeMiddleware((req, res, next) => { next(); });

Solution

  1. Step 1: Recall Express middleware syntax

    Express uses app.use() to add middleware functions.
  2. Step 2: Check function parameters

    Middleware functions receive req, res, next and must call next() to continue.
  3. Final Answer:

    app.use((req, res, next) => { console.log('Middleware'); next(); }); -> Option C
  4. Quick Check:

    app.use with (req, res, next) and next() [OK]
Hint: Middleware uses app.use with three parameters and calls next() [OK]
Common Mistakes:
  • Using non-existent methods like app.middleware
  • Omitting the next parameter or call
  • Confusing middleware with route handlers
3. Consider this Express code snippet:
app.use((req, res, next) => { console.log('First'); next(); });
app.use((req, res, next) => { console.log('Second'); next(); });
app.get('/', (req, res) => { res.send('Hello'); });

What will be printed in the console when a GET request is made to '/'?
medium
A. Second\nFirst
B. First\nSecond
C. Hello
D. No output

Solution

  1. Step 1: Understand middleware execution order

    Middleware runs in the order they are added with app.use().
  2. Step 2: Trace console logs

    First middleware logs 'First' then calls next(), second logs 'Second' then calls next(), then route sends response.
  3. Final Answer:

    First\nSecond -> Option B
  4. Quick Check:

    Middleware executes in order added [OK]
Hint: Middleware logs appear in order of app.use calls [OK]
Common Mistakes:
  • Assuming route handler logs to console
  • Thinking middleware runs after route
  • Mixing order of middleware execution
4. What is the problem with this middleware code?
app.use((req, res, next) => { console.log('Middleware running'); });
app.get('/', (req, res) => { res.send('Done'); });
medium
A. Middleware should be added after routes
B. Middleware syntax is incorrect
C. Route handler is missing parameters
D. Missing call to next(), request will hang

Solution

  1. Step 1: Check middleware function

    The middleware logs a message but does not call next().
  2. Step 2: Understand effect of missing next()

    Without calling next(), the request does not proceed to the route handler, causing the request to hang.
  3. Final Answer:

    Missing call to next(), request will hang -> Option D
  4. Quick Check:

    Middleware must call next() to continue [OK]
Hint: Always call next() in middleware to avoid hanging requests [OK]
Common Mistakes:
  • Forgetting to call next() in middleware
  • Thinking middleware runs after routes
  • Assuming middleware syntax is wrong without checking next()
5. You want to create middleware that only runs for routes starting with '/admin'. Which is the correct way to apply this middleware?
hard
A. app.use('/admin', (req, res, next) => { console.log('Admin middleware'); next(); });
B. app.use((req, res, next) => { if(req.url.startsWith('admin')) { console.log('Admin middleware'); } next(); });
C. app.get('/admin', (req, res, next) => { console.log('Admin middleware'); next(); });
D. app.use('*admin', (req, res, next) => { console.log('Admin middleware'); next(); });

Solution

  1. Step 1: Understand path filtering in middleware

    Express allows specifying a path prefix in app.use(path, middleware) to run middleware only on matching routes.
  2. Step 2: Evaluate options

    app.use('/admin', (req, res, next) => { console.log('Admin middleware'); next(); }); correctly uses app.use('/admin', ...) for prefix matching. app.use((req, res, next) => { if(req.url.startsWith('admin')) { console.log('Admin middleware'); } next(); }); has incorrect condition (startsWith('admin') misses leading /, never matches). app.get('/admin', (req, res, next) => { console.log('Admin middleware'); next(); }); is a route handler, not middleware. app.use('*admin', (req, res, next) => { console.log('Admin middleware'); next(); }); *admin matches paths containing admin at the end, not starting with /admin.
  3. Final Answer:

    app.use('/admin', (req, res, next) => { console.log('Admin middleware'); next(); }); -> Option A
  4. Quick Check:

    Use app.use with path prefix for selective middleware [OK]
Hint: Use app.use('/path', middleware) to target specific routes [OK]
Common Mistakes:
  • Using route handlers instead of middleware
  • Incorrect conditional checks in global middleware
  • Wrong path patterns like wildcards in wrong position
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