In this article, we’ll learn useful information about request objects in Express and their properties.
When the user (client) accesses a route, the user sends the Express application a Request.
The request object, often abbreviated as req, represents the HTTP request property.
We can access any data sent with the body, such as URL parameters, query strings, and HTTP headers.
Property | Description |
---|---|
req.params | An object containing properties mapped to the named route “parameters” |
req.query | An object containing a property for each query string parameter in the route. |
req.body | Contains key-value pairs of data submitted in the request body |
req.protocol | Contains the request protocol string (http or https). |
req.hostname | Contains the hostname derived from the Host HTTP header. |
req.path | Contains the path part of the request URL. |
req.originalUrl | Contains the entire request url. |
req.subdomains | An array of subdomains in the domain name of the request. |
req.header | An HTTP header that can be used in an HTTP response. |
req.cookies | An object containing cookies sent by the request |
res.ip | Contains the remote IP address of the request. |
There are a few ways for us to receive data from users. For example, we can use req.params, req.query, and req.body.
req.params
// GET https://example.com/user/123
app.get("user/:id", (req, res) => {
console.log(req.params.id); // "123"
});
req.query
// GET https://example.com/user?userID=123&action=submit
app.get("user/", (req, res) => {
console.log(req.query.userID); // "123"
console.log(req.query.action); // "submit"
});
req.body
// POST https://example.com/login {username: "anna", password: "1234"}
app.get("login/", (req, res) => {
console.log(req.body.username); // "anna"
console.log(req.body.password); // "1234"
});
We can access an object that stores URL information such as protocol, hostname, path, and subdomains with a res object to handle URL-related tasks.
// https://learn.devhandbook.com/search?keyword=express
app.get("/search", (req, res) => {
console.log(req.protocol); // "https"
console.log(req.hostname); // "devhandbook.com"
console.log(req.path); // "/search"
console.log(req.originalUrl); // "/keyword=express"
console.log(req.subdomains); // "["learn"]"
});
In addition to sending data and users, the browser also sends headers containing additional information such as content-type, content-length, and other information.
app.post("/login", (req, res) => {
req.header("Content-Type");
req.header("Content-Length");
req.header("user-agent");
req.header("Authorization");
});
When using cookie-parse middleware, the req.cookies object will include the user’s cookies.
If the request contains no cookies, it defaults to {}.
app.post("/login", (req, res) => {
req.cookies.isShowPopup;
req.cookies.sessionID;
});