0
0
Expressframework~20 mins

Conditional middleware execution in Express - Practice Problems & Coding Challenges

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!
component_behavior
intermediate
2:00remaining
Middleware execution based on request path
Consider this Express middleware setup. What will be the output when a GET request is made to /api/data?
Express
const express = require('express');
const app = express();

function logMiddleware(req, res, next) {
  console.log('Middleware triggered');
  next();
}

app.use((req, res, next) => {
  if (req.path.startsWith('/api')) {
    logMiddleware(req, res, next);
  } else {
    next();
  }
});

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

app.listen(3000);
AConsole logs nothing and client receives 404 Not Found.
BConsole logs nothing and client receives 'Data response'.
CConsole logs 'Middleware triggered' but client hangs without response.
DConsole logs 'Middleware triggered' and client receives 'Data response'.
Attempts:
2 left
💡 Hint
Think about when the middleware function is called based on the request path.
📝 Syntax
intermediate
2:00remaining
Correct conditional middleware syntax
Which option correctly applies middleware only for POST requests to '/submit' in Express?
Aapp.post('/submit', (req, res, next) => { next(); });
Bapp.use((req, res, next) => { if (req.path === '/submit' && req.method === 'POST') next(); else next(); });
Capp.use('/submit', (req, res, next) => { if (req.method === 'POST') next(); else res.sendStatus(405); });
D;)} ;)504(sutatSdnes.ser esle ;)(txen )'TSOP' === dohtem.qer( fi { >= )txen ,ser ,qer( ,'timbus/'(esu.ppa
Attempts:
2 left
💡 Hint
Remember the difference between app.use and app.post for method-specific middleware.
🔧 Debug
advanced
2:00remaining
Middleware not executing as expected
Given this code, why does the middleware never log 'Middleware active' when accessing '/dashboard'?
Express
const express = require('express');
const app = express();

app.use('/dashboard', (req, res, next) => {
  console.log('Middleware active');
  next();
});

app.get('/dashboard', (req, res) => {
  res.send('Dashboard page');
});

app.listen(3000);
ABecause the middleware is applied after the route handler, so it never runs.
BBecause app.use with a path only matches requests starting with that path, so it should work and the problem is elsewhere.
CBecause the middleware is applied to '/dashboard' but the request path includes a trailing slash '/dashboard/', so it doesn't match.
DBecause the middleware is synchronous and blocks the route handler.
Attempts:
2 left
💡 Hint
Check how Express matches paths with and without trailing slashes.
state_output
advanced
2:00remaining
Middleware modifying request state conditionally
What will be the value of req.user in the route handler after this middleware runs for a request to '/profile' with header Authorization: Bearer token123?
Express
const express = require('express');
const app = express();

app.use((req, res, next) => {
  if (req.headers.authorization?.startsWith('Bearer ')) {
    req.user = { id: 1, name: 'Alice' };
  }
  next();
});

app.get('/profile', (req, res) => {
  res.json(req.user || null);
});

app.listen(3000);
A{"id":1,"name":"Alice"}
Bnull
Cundefined
DThrows a TypeError
Attempts:
2 left
💡 Hint
Look at how the middleware sets req.user based on the Authorization header.
🧠 Conceptual
expert
3:00remaining
Understanding conditional middleware order and effect
In Express, if you want to run a logging middleware only for routes starting with '/admin' and ensure it runs before any route handler, which setup is correct?
APlace <code>app.use(loggerMiddleware)</code> after all '/admin' routes are defined and check path inside middleware.
BDefine <code>app.get('/admin/*', loggerMiddleware, handler)</code> for each route individually.
CPlace <code>app.use('/admin', loggerMiddleware)</code> before defining any '/admin' routes.
DUse <code>app.use((req, res, next) => { if (req.path.startsWith('/admin')) loggerMiddleware(req, res, next); else next(); })</code> after routes.
Attempts:
2 left
💡 Hint
Middleware order matters in Express; earlier middleware runs before later routes.