Built-in middleware helps your Express app understand and handle different types of data easily. It also helps serve files like images or stylesheets to users.
When you want to read JSON data sent from a web form or API.
When you need to process data sent from a form using URL-encoded format.
When you want to serve images, CSS, or JavaScript files directly to users.
express.urlencoded() parses URL-encoded form data.
Examples
This lets your app read JSON data sent in the request body.
Express
app.use(express.json())
This parses simple URL-encoded form data.
Express
app.use(express.urlencoded({ extended: false }))
This serves files from the 'assets' folder to users.
Express
app.use(express.static('assets'))
Sample Program
This Express app uses built-in middleware to handle JSON and URL-encoded data. It also serves static files from the 'public' folder. You can test sending JSON to /json and form data to /form.
Express
import express from'express'
const app = express()
const port = 3000
// Middleware to parse JSON data
app.use(express.json())
// Middleware to parse URL-encoded data
app.use(express.urlencoded({ extended: true }))
// Middleware to serve static files from'public' folder
app.use(express.static('public'))
// Route to test JSON data parsing
app.post('/json', (req, res) => {
res.send(`Received JSON with name: ${req.body.name}`)
})
// Route to test URL-encoded data parsing
app.post('/form', (req, res) => {
res.send(`Received form data with email: ${req.body.email}`)
})
app.listen(port, () => {
console.log(`Server running on http://localhost:${port}`)
})
OutputSuccess
Important Notes
Use express.json() before routes that need JSON data.
Use express.urlencoded() to handle form submissions.
express.static() helps serve files like images or CSS without extra code.
Summary
Built-in middleware makes handling data and files easy in Express.
express.json() parses JSON request bodies.
express.urlencoded() parses form data.
express.static() serves static files.
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
Step 1: Understand middleware purpose
express.json() is designed to parse JSON data sent in request bodies.
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.
Final Answer:
express.json() -> Option A
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
Step 1: Recall middleware syntax
express.urlencoded() is a function that returns middleware and accepts an options object.
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.
Final Answer:
app.use(express.urlencoded({ extended: true })); -> Option C
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?
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
Step 1: Understand express.static middleware
express.static('public') serves files from the 'public' folder when requested by path.
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.
Step 3: Check the actual request path
Requesting /public/image.png will cause 404 because the middleware serves files at root, not under /public.
Final Answer:
404 Not Found error because no route matches /public/image.png. -> Option A
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: