了解如何使用Express访问和改变HTTP头信息
从一个请求中访问HTTP头信息值
你可以使用Request.headers 属性访问所有的HTTP头信息。
app.get('/', (req, res) => {
console.log(req.headers)
})
使用Request.header() 方法来访问一个单独的请求头的值。
app.get('/', (req, res) => {
req.header('User-Agent')
})
改变一个响应的任何HTTP头的值
你可以使用Response.set() 来改变任何HTTP头的值。
res.set('Content-Type', 'text/html')
不过,对于Content-Type头,有一个快捷方式。
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: