Challenge - 5 Problems
Middleware Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What is the output when using this middleware setup?
Consider this Express app code snippet. What will be the response when a GET request is made to '/'?
Express
import express from 'express'; import helmet from 'helmet'; const app = express(); app.use(helmet()); app.get('/', (req, res) => { res.send('Hello World'); }); app.listen(3000);
Attempts:
2 left
💡 Hint
Helmet adds security headers but does not block routes.
✗ Incorrect
Helmet is a middleware that adds security headers to responses. It does not block routes or crash the server if used with default settings. The GET '/' route sends 'Hello World' as response, enhanced by helmet headers.
📝 Syntax
intermediate2:00remaining
Which option correctly installs and uses the 'cors' middleware?
You want to enable Cross-Origin Resource Sharing (CORS) in your Express app using the 'cors' package. Which code snippet correctly installs and applies the middleware?
Attempts:
2 left
💡 Hint
Use ES module import and call the middleware function.
✗ Incorrect
Option B correctly imports 'cors' using ES module syntax and calls it as a function in app.use. Options A and B use CommonJS require which is invalid if using ES modules. Option B forgets to call cors as a function.
🔧 Debug
advanced2:00remaining
Why does this middleware not run as expected?
Given this Express app code, why does the custom middleware not log requests?
Express
import express from 'express'; const app = express(); app.get('/', (req, res) => { res.send('Home'); }); app.use((req, res, next) => { console.log('Request received'); next(); }); app.listen(3000);
Attempts:
2 left
💡 Hint
Middleware order matters in Express.
✗ Incorrect
Express runs middleware and routes in the order they are defined. Since the middleware is defined after the '/' route, requests to '/' are handled before the middleware runs, so the log never appears.
❓ state_output
advanced2:00remaining
What is the output of this middleware chain?
Analyze this Express app code. What will be the response body when a GET request is made to '/'?
Express
import express from 'express'; const app = express(); app.use((req, res, next) => { req.customValue = 5; next(); }); app.use((req, res, next) => { req.customValue += 10; next(); }); app.get('/', (req, res) => { res.send(`Value is ${req.customValue}`); }); app.listen(3000);
Attempts:
2 left
💡 Hint
Middleware can add and modify request properties.
✗ Incorrect
The first middleware sets req.customValue to 5. The second adds 10, making it 15. The route sends 'Value is 15'.
🧠 Conceptual
expert2:00remaining
Which statement about third-party middleware installation is true?
Select the correct statement about installing and using third-party middleware in Express.
Attempts:
2 left
💡 Hint
Think about how Node.js packages work.
✗ Incorrect
Third-party middleware must be installed with npm or yarn and imported before use. Middleware order matters and usually middleware is placed before routes. Express does not auto-install packages.