Challenge - 5 Problems
Express Middleware Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What is the output when sending JSON data with express.json() middleware?
Consider this Express server code snippet:
If a client sends a POST request to
const express = require('express');
const app = express();
app.use(express.json());
app.post('/data', (req, res) => {
res.json({ received: req.body });
});
// Server listens on port 3000If a client sends a POST request to
/data with JSON body {"name":"Alice"}, what will the server respond with?Express
const express = require('express'); const app = express(); app.use(express.json()); app.post('/data', (req, res) => { res.json({ received: req.body }); });
Attempts:
2 left
💡 Hint
Think about what express.json() middleware does to the request body.
✗ Incorrect
The express.json() middleware parses the incoming JSON string into a JavaScript object and assigns it to req.body. So the server responds with the parsed object inside the 'received' key.
📝 Syntax
intermediate2:00remaining
Which option correctly uses express.urlencoded() middleware?
You want to parse URL-encoded form data in an Express app. Which code snippet correctly sets up the middleware?
Attempts:
2 left
💡 Hint
The 'extended' option must be a boolean.
✗ Incorrect
express.urlencoded() requires the 'extended' option to be true or false. Option D correctly sets it to true, enabling rich objects and arrays to be encoded.
🔧 Debug
advanced2:00remaining
Why does static middleware not serve files as expected?
Given this Express setup:
The 'public' folder contains 'index.html'. When visiting '/', the browser shows 'Home page' instead of the static file. Why?
app.use(express.static('public'));
app.get('/', (req, res) => {
res.send('Home page');
});The 'public' folder contains 'index.html'. When visiting '/', the browser shows 'Home page' instead of the static file. Why?
Attempts:
2 left
💡 Hint
Think about middleware order and route matching.
✗ Incorrect
Express matches routes in order. The route handler for '/' matches the root path and sends 'Home page' before static middleware can serve 'index.html'.
❓ state_output
advanced2:00remaining
What is the response when sending URL-encoded data without urlencoded middleware?
Consider this Express app:
If a client sends a POST request to
const express = require('express');
const app = express();
app.post('/submit', (req, res) => {
res.json({ data: req.body });
});If a client sends a POST request to
/submit with Content-Type application/x-www-form-urlencoded and body name=Bob&age=30, what will the response be?Express
const express = require('express'); const app = express(); app.post('/submit', (req, res) => { res.json({ data: req.body }); });
Attempts:
2 left
💡 Hint
What happens if you don't use urlencoded middleware for form data?
✗ Incorrect
Without urlencoded middleware, Express does not parse URL-encoded form data, so req.body remains an empty object.
🧠 Conceptual
expert3:00remaining
Which middleware order correctly serves static files and parses JSON and URL-encoded bodies?
You want to set up an Express app that:
- Serves static files from 'public'
- Parses JSON request bodies
- Parses URL-encoded form data
Which middleware order is correct to ensure all features work as expected?
- Serves static files from 'public'
- Parses JSON request bodies
- Parses URL-encoded form data
Which middleware order is correct to ensure all features work as expected?
Attempts:
2 left
💡 Hint
Static files should be served after body parsing middleware to avoid conflicts.
✗ Incorrect
Middleware order matters. Parsing middleware should come before static middleware to ensure request bodies are parsed for routes, while static files are served for matching paths.