Learn how to use Express to access and change HTTP headers
Access HTTP header value from request
You can use to access all HTTP headersRequest.headers
property:
app.get('/', (req, res) => {
console.log(req.headers)
})
useRequest.header()
Ways to access a single request header value:
app.get('/', (req, res) => {
req.header('User-Agent')
})
Change any HTTP header value of the response
You can change any HTTP header value usingResponse.set()
:
res.set('Content-Type', 'text/html')
However, there is a shortcut for the Content-Type header:
res.type('.html')
// => 'text/html'
res.type('html')
// => 'text/html'
res.type('json')
// => 'application/json'
res.type('application/json')
// => 'application/json'
res.type('png')
// => image/png:
Download mine for freeExpress.js manual
More crash tutorials:
- Express, the popular Node.js framework
- Use Express to retrieve GET query string parameters
- Use express-validator to validate input in Express
- Express template
- Use Express to serve static assets
- Send JSON response using Express
- Fast meeting
- Send a reply using Express
- Send files using Express
- Use Express-Validator to clean up the input in Express
- Route in Express
- Express HTTPS server with self-signed certificate
- Express, request parameters
- Use Express to retrieve POST query parameters
- Use Express to handle redirects
- Fast middleware
- Set up let's encrypt for Express
- Use HTTP headers in Express
- Processing forms in Express
- Use Express to process file uploads in forms
- Processing CORS in Express
- Use Express to manage cookies