Bird
Raised Fist0
Expressframework~20 mins

Built-in middleware (json, urlencoded, static) 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 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.

Practice

(1/5)
1. Which built-in Express middleware is used to parse incoming JSON request bodies?
easy
A. express.json()
B. express.urlencoded()
C. express.static()
D. express.bodyParser()

Solution

  1. Step 1: Understand middleware purpose

    express.json() is designed to parse JSON data sent in request bodies.
  2. Step 2: Compare with other middleware

    express.urlencoded() parses URL-encoded form data, express.static() serves files, and express.bodyParser() is not a built-in middleware.
  3. Final Answer:

    express.json() -> Option A
  4. Quick Check:

    JSON parsing = express.json() [OK]
Hint: JSON data uses express.json() middleware [OK]
Common Mistakes:
  • Confusing urlencoded with json middleware
  • Using express.static() for parsing data
  • Assuming bodyParser is built-in
2. Which of the following is the correct way to use Express middleware to parse URL-encoded form data with support for rich objects and arrays (extended: true)?
easy
A. app.use(express.urlencoded);
B. app.use(express.urlencoded());
C. app.use(express.urlencoded({ extended: true }));
D. app.use(express.urlencoded(true));

Solution

  1. Step 1: Recall middleware syntax

    express.urlencoded() is a function that returns middleware and accepts an options object.
  2. Step 2: Identify correct usage

    The correct usage includes calling the function with an options object, commonly { extended: true } to support rich objects and arrays.
  3. Final Answer:

    app.use(express.urlencoded({ extended: true })); -> Option C
  4. Quick Check:

    Call urlencoded() with options object [OK]
Hint: Call express.urlencoded() with options object [OK]
Common Mistakes:
  • Forgetting parentheses after express.urlencoded
  • Passing boolean directly instead of options object
  • Using express.urlencoded without calling it
3. Given the code below, what will be the output when a GET request is made to /public/image.png if the file exists?
const express = require('express');
const app = express();
app.use(express.static('public'));
app.get('/', (req, res) => res.send('Home'));
app.listen(3000);
medium
A. 404 Not Found error because no route matches /public/image.png.
B. The server will respond with 'Home' text.
C. Syntax error due to missing middleware.
D. The image file image.png will be served from the public folder.

Solution

  1. Step 1: Understand express.static middleware

    express.static('public') serves files from the 'public' folder when requested by path.
  2. Step 2: Analyze request path

    A GET request to /public/image.png looks for public/public/image.png which does not exist. But since the middleware serves files relative to the folder, the correct URL to access image.png is /image.png.
  3. Step 3: Check the actual request path

    Requesting /public/image.png will cause 404 because the middleware serves files at root, not under /public.
  4. Final Answer:

    404 Not Found error because no route matches /public/image.png. -> Option A
  5. Quick Check:

    express.static serves files at root URL [OK]
Hint: Static files served at root URL, not under folder name [OK]
Common Mistakes:
  • Assuming static serves files under /folder path
  • Expecting route handler to serve static files
  • Confusing middleware path with URL path
4. Identify the error in the following Express setup code:
const express = require('express');
const app = express();
app.use(express.json);
app.use(express.urlencoded({ extended: false }));
app.listen(3000);
medium
A. express.json is not a valid middleware.
B. Missing parentheses after express.json middleware call.
C. Incorrect option passed to express.urlencoded.
D. app.listen should be called before middleware.

Solution

  1. Step 1: Check middleware usage syntax

    Middleware functions like express.json() must be called with parentheses to return the middleware function.
  2. Step 2: Identify the error

    The code uses express.json without parentheses, so it passes the function itself, not the middleware, causing an error.
  3. Final Answer:

    Missing parentheses after express.json middleware call. -> Option B
  4. Quick Check:

    Call express.json() with () [OK]
Hint: Always call middleware functions with parentheses [OK]
Common Mistakes:
  • Forgetting () after express.json
  • Passing wrong options to urlencoded
  • Calling app.listen before middleware
5. You want to serve static files from the folder named assets under the URL path /static. Which Express middleware setup achieves this correctly?
hard
A. app.use(express.static('assets/static'));
B. app.use(express.static('/static/assets'));
C. app.use('/assets', express.static('static'));
D. app.use('/static', express.static('assets'));

Solution

  1. Step 1: Understand express.static usage with mount path

    To serve files from a folder under a specific URL path, use app.use('/urlpath', express.static('folder')).
  2. Step 2: Match folder and URL path

    Here, the folder is assets and the URL path is /static, so app.use('/static', express.static('assets')) is correct.
  3. Step 3: Analyze incorrect options

    Other options either swap folder and path or use incorrect paths causing errors or wrong URLs.
  4. Final Answer:

    app.use('/static', express.static('assets')); -> Option D
  5. Quick Check:

    Mount static folder on desired URL path [OK]
Hint: Mount static folder with URL path as first argument [OK]
Common Mistakes:
  • Swapping folder and URL path arguments
  • Using absolute paths incorrectly
  • Not specifying URL path for static files
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