Getting the Request Body in Express
Nội Dung Chính
Getting the Request Body in Express
Express doesn’t automatically parse the HTTP request body for you,
but it does have an officially supported middleware package for parsing HTTP request bodies. As of v4.16.0, Express comes with a built-in JSON request body parsing middleware that’s good enough for most JavaScript apps.
JSON Request Body
Express has a built-in express.json()
function that returns an Express middleware function that parses JSON HTTP request bodies into
JavaScript objects. The json()
middleware
adds a body
property to the Express request req
.
To access the parsed request body, use req.body
as shown below.
const
express = require
('express'
);
const
app = express();
app.use(express.json());
app.post('*'
, (req, res) => {
req.body;
res.json(req.body);
});
const
server = await
app.listen(3000
);
const
axios = require
('axios'
);
const
res = await
axios.post('http://localhost:3000/'
, {
answer
: 42
});
res.data;
Common Gotchas
If the JSON body is malformed, Express will error out with an HTTP 400. This
error also triggers error handling middleware.
const
express = require
('express'
);
const
app = express();
app.use(express.json());
app.post('*'
, (req, res) => {
res.json(req.body);
});
app.use(function
(err, req, res, next
) {
err.message;
next(err);
});
const
server = await
app.listen(3000
);
const
axios = require
('axios'
);
const
headers = { 'Content-Type'
: 'application/json'
};
const
err = await
axios.
post('http://localhost:3000/'
, 'not json'
, { headers }).
then(()
=> null
, err => err);
err.response.status;
err.message;
It is important to note that, by default, the json()
middleware ignores
any request whose Content-Type
header isn’t something that Express
recognizes as JSON. If express.json()
is silently ignoring your request,
make sure you check the Content-Type
header.
const
express = require
('express'
);
const
app = express();
app.use(express.json());
app.post('*'
, (req, res) => {
req.body;
res.json(req.body);
});
const
server = await
app.listen(3000
);
const
axios = require
('axios'
);
const
headers = { 'Content-Type'
: 'text/plain'
};
const
res = await
axios.
post('http://localhost:3000/'
, 'not json'
, { headers });
res.data;
URL-Encoded Form Body Parser
Express has an officially supported module body-parser that includes a
parser for URL-encoded request bodies, like the ones submitted by HTML forms.
const
express = require
('express'
);
const
app = express();
app.use(require
('body-parser'
).urlencoded({ extended
: false
}));
app.post('*'
, (req, res) => {
req.body;
res.json(req.body);
});
const
server = await
app.listen(3000
);
const
axios = require
('axios'
);
const
headers = {
'Content-Type'
: 'application/x-www-form-urlencoded'
};
const
res = await
axios.
post('http://localhost:3000/'
, 'answer=42'
, { headers });
res.data;
Files
Neither Express nor body-parser supports file uploads out of the box.
However, you can use the Formidable module on npm to handle file uploads. You can learn how
on our tutorial on file uploads with Express.
Want to become your team’s Express expert? There’s no better way to really grok a framework than to write your own
clone from scratch. In 15 concise pages, this tutorial walks you through how to write a simplified clone of Express
called Espresso.
Get your copy!
Espresso supports:
- Route handlers, like `app.get()` and `app.post()`
- Express-compatible middleware, like `app.use(require(‘cors’)())`
- Express 4.0 style subrouters
As a bonus, Espresso also supports async functions, unlike Express.
Get the tutorial and master Express today!