The request object lets us get the information about requests made from the client in middlewares and route handlers.
In this article, we’ll look at the properties of Express’s request object methods in detail, including getting headers and query strings.
Methods
The request object has various methods.
req.accepts(types)
The accepts
method checks the Accept
request header for the value that’s sent with the header. It takes a string with the value of the data type that we want to check if it’s sent with the Accept
request header.
It returns the best match, or if nothing matches, false
is returned.
If false
is returned, then the 406 response code should be returned since the data type isn’t accepted by our route.
For example, we can use it as follows:
const express = require('express');
const bodyParser = require('body-parser');
const app = express();
app.get('/', (req, res) => {
res.send(req.accepts('html'));
})
app.listen(3000);
Then when we send the Accept
header with a value that has html
in it, then it’ll return html
.
Otherwise, false
will be returned.
Other examples include:
req.accepts('text/html')
req.accepts(['json', 'text'])
req.accepts('application/json')
req.accepts('image/png')
req.accepts('png')
req.accepts(['html', 'json'])
req.acceptsCharsets(charset [, …]), req.acceptsEncodings(encoding [, …]), req.acceptsLanguages(lang [, …])
The method returns the first accepted charset of the specified character set. The charset is from the request’s Accept-Charset
HTTP header field.
For example, we can use it as follows:
const express = require('express');
const bodyParser = require('body-parser');
const app = express();
app.get('/', (req, res) => {
res.send(req.acceptsCharsets('utf8'));
})
app.listen(3000);
Then we get back the character set in the Accept-Charset
HTTP header if it’s set.
acceptsEncodings
returns the encoding based on the Accepts-Encoding
HTTP header; and acceptsLanguages
returns the encoding based on the Accepts-Language
HTTP header.
req.get(field)
We can get the header field value with the get
method. It takes the key of the header and it’s case-insensitive.
For example, we can use it as follows:
const express = require('express');
const bodyParser = require('body-parser');
const app = express();
app.get('/', (req, res) => {
res.send(req.get('content-type'));
})
app.listen(3000);
Then when we make a request to the /
route with the Content-Type
header’s value set to text/cmd
, then we get back text/cmd
.
req.is(type)
The is
method returns the Content-Type
header’s value if it matches the type
passed in. If the request has no body, the null
is returned. Otherwise, false
is returned.
For example, we can use it as follows:
const express = require('express');
const bodyParser = require('body-parser');
const app = express();
app.get('/', (req, res) => {
res.send(req.is('html'));
})
app.listen(3000);
Then we get null
since it’s a GET request, which has no request body.
On the other hand, if we have:
const express = require('express');
const bodyParser = require('body-parser');
const app = express();
app.post('/', (req, res) => {
res.send(req.is('html'));
})
app.listen(3000);
When we make a POST request to the/
route with the Content-Type
header set to text/html
, then we get back html
.
If the Content-Type
header is set to something without html
, then we get back false
.
req.param(name [, defaultValue])
req.param
gets the value from the query string’s search parameter with the given key.
For example, we can use it as follows:
const express = require('express');
const bodyParser = require('body-parser');
const app = express();
app.post('/', (req, res) => {
res.send(req.param('name', 'Joe'));
})
app.listen(3000);
The code above get the search parameter value with the name
key. The default value is set to 'Joe'
.
Then when we make a request to /?name=jane
, then we get jane
. Otherwise, if the name
search parameter isn’t specified, then we get Joe
.
req.param
is deprecated, so we should use req.params
, req.body
or req.query
, as applicable instead.
Lookup is of query string, URL parameter and body values are performed in the following order:
req.params
req.body
req.query
req.range(size[, options])
The range
method parses the Range
header. The size
parameter has the maximum size of the resource. The options
parameter is an object which can have the following property:
combine
— a boolean that specifies if overlapping and adjacent ranges should be combined.
For example, we can use it as follows:
const express = require('express');
const bodyParser = require('body-parser');
const app = express();app.get('/', (req, res) => {
const range = req.range(20000);
if (range && range.type === 'bytes') {
res.json(range);
}
else {
res.send();
}
})app.listen(3000);
Then when we make a GET request to /
with the Range
header set to bytes=200–1000, 2000–6576, 19000-
, we get:
[
{
"start": 200,
"end": 1000
},
{
"start": 2000,
"end": 6576
},
{
"start": 19000,
"end": 19999
}
]
since we specified the size
parameter to be 20000, so the last end
value is 19999.
If we didn’t specify bytes
in the value, we get nothing.
Conclusion
The request object has methods to parse query strings and get various headers like the Range
, Accepts
and Content-Type
headers.
We can get the Range
header with range
, Accepts
with accepts
and Content-Type
with is
.
The get
method can get any request header field’s value.