If you're diving into building web applications with Node.js and Express, you've probably encountered the req
(short for request) object. The req
object is crucial in handling HTTP requests, as it provides all the data sent by the client to your server. In this article, we'll explore five essential methods on the req
object that every Express developer should know.
1. req.param(paramName)
The req.param()
method retrieves route parameters, query string parameters, or body parameters by name. While it's a quick way to access parameters, it’s important to note that this method is now deprecated and you should use more specific methods (req.params
, req.query
, or req.body
).
Example:
app.get('/users/:id', (req, res) => {
const userId = req.param('id'); // Deprecated, use req.params.id
res.send(`User ID: ${userId}`);
});
Best Practice:
Instead, use:
const userId = req.params.id;
2. req.params
This property contains route parameters (parameters defined in the route path). If you have dynamic segments in your route, req.params
is your go-to method for accessing them.
Example:
app.get('/users/:id', (req, res) => {
const userId = req.params.id;
res.send(`User ID: ${userId}`);
});
In the route /users/42
, req.params.id
will return 42
.
3. req.query
The req.query
method retrieves query string parameters. These are the key-value pairs sent in the URL after the ?
symbol.
Example:
app.get('/search', (req, res) => {
const { term, page } = req.query;
res.send(`Search Term: ${term}, Page: ${page}`);
});
For the URL /search?term=express&page=2
, req.query.term
will return express
and req.query.page
will return 2
.
4. req.body
The req.body
method is used to access the body of a request, typically for POST, PUT, or PATCH requests. However, to use req.body
, you need to use middleware like express.json()
or express.urlencoded()
to parse incoming request bodies.
Example:
app.use(express.json());
app.post('/login', (req, res) => {
const { username, password } = req.body;
res.send(`Username: ${username}, Password: ${password}`);
});
For a JSON request body like { "username": "john", "password": "12345" }
, req.body.username
will return john
.
5. req.get(headerName)
The req.get()
method retrieves the value of a specific HTTP header sent by the client.
Example:
app.get('/info', (req, res) => {
const userAgent = req.get('User-Agent');
res.send(`User-Agent: ${userAgent}`);
});
If a client sends a User-Agent
header, req.get('User-Agent')
will return its value.
Bonus: req.ip
While not a method, the req.ip
property is incredibly useful for getting the client's IP address. This can be helpful for logging or security purposes.
Example:
app.get('/ip', (req, res) => {
res.send(`Your IP address is: ${req.ip}`);
});
Wrapping Up
Understanding these req
object methods can significantly enhance how you handle incoming requests in your Express applications. While some methods like req.param()
are deprecated, alternatives like req.params
, req.query
, and req.body
provide a structured way to access different parts of the request. Happy coding!
What are your favorite req
methods or tips for working with Express? Share them in the comments below!
Top comments (0)