Express req Object
What you’ll learn
- How Express builds and exposes the
reqobject. - Which commonly used properties to read in real handlers.
- When to use headers, params, query, and body accessors.
- How to safely validate request data before processing.
Common req properties and methods
javascript
req.method
req.path
req.params
req.query
req.body
req.get('Header-Name')1
Read request data in one handler
javascript
app.post('/users/:id', function (req, res) {
res.json({
method: req.method,
id: req.params.id,
page: req.query.page,
body: req.body
});
});2
Validate incoming request inputs
javascript
app.get('/items/:id', function (req, res) {
var id = Number(req.params.id);
if (!Number.isInteger(id)) return res.status(400).send('Invalid id');
res.send('Item ' + id);
});❓ FAQ
req is the incoming request object available in middleware and route handlers.
It contains request metadata such as method, path, params, query, body, headers, and more.
Express builds on Node's object and adds many convenience properties and methods.
Use req.params for route segments and req.query for query string values.
Yes. Treat body, params, query, and headers as untrusted input and validate before use.
Did you know?
The Express req object extends Node’s request object and centralizes URL, headers, body, params, query, and routing context.
4 people found this page helpful
