0
0
Expressframework~20 mins

Built-in middleware (json, urlencoded, static) 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
What is the output when sending JSON data with express.json() middleware?
Consider this Express server code snippet:
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 3000

If 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 });
});
A{"received":{"name":"Alice"}}
B{"received":"{\"name\":\"Alice\"}"}
CSyntaxError: Unexpected token n in JSON at position 1
D{"received":null}
Attempts:
2 left
💡 Hint
Think about what express.json() middleware does to the request body.
📝 Syntax
intermediate
2: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?
Aapp.use(express.urlencoded());
Bapp.use(express.urlencoded({ extended: 'yes' }));
Capp.use(express.urlencoded({ extended: false }));
Dapp.use(express.urlencoded({ extended: true }));
Attempts:
2 left
💡 Hint
The 'extended' option must be a boolean.
🔧 Debug
advanced
2:00remaining
Why does static middleware not serve files as expected?
Given this Express setup:
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?
Aexpress.static requires an absolute path, 'public' is invalid.
BThe static middleware must be placed after the route handler to work.
CThe static middleware only serves files for matching paths, '/' matches the route handler first.
DStatic middleware only serves files with .js or .css extensions by default.
Attempts:
2 left
💡 Hint
Think about middleware order and route matching.
state_output
advanced
2:00remaining
What is the response when sending URL-encoded data without urlencoded middleware?
Consider this Express app:
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 });
});
A{"data":null}
B{"data":{}}
CSyntaxError: Unexpected token n in JSON at position 1
D{"data":{"name":"Bob","age":"30"}}
Attempts:
2 left
💡 Hint
What happens if you don't use urlencoded middleware for form data?
🧠 Conceptual
expert
3: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?
A
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.use(express.static('public'));
B
app.use(express.static('public'));
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
C
app.use(express.urlencoded({ extended: true }));
app.use(express.static('public'));
app.use(express.json());
D
app.use(express.urlencoded({ extended: true }));
app.use(express.json());
app.use(express.static('public'));
Attempts:
2 left
💡 Hint
Static files should be served after body parsing middleware to avoid conflicts.