我想获得一个请求的“主机”头使用Node JS的连接库包。我的代码如下:
var app = connect()
.use(connect.logger('dev'))
.use(connect.static('public'))
.use(function(req, res){
var host = req.???
})
.listen(3000);
connect的文档在这里,但我在上面的代码中没有看到任何详细说明req对象的API。http://www.senchalabs.org/connect/
编辑:注意,一个成功的答案必须指向文档(我需要这个来验证哪个版本提供了我正在寻找的API)。
logger.info({headers:req.headers})
输出;
"headers":{"authorization":"Basic bmluYWQ6bmluYWQ=","content-
type":"application/json","user-
agent":"PostmanRuntime/7.26.8","accept":"*/*","postman-token":"36e0d84a-
55be-4661-bb1e-1f04d9499574","host":"localhost:9012","accept-
encoding":"gzip, deflate, br","connection":"keep-alive","content-
length":"198"}
要查看HTTP请求头的列表,您可以使用:
console.log(JSON.stringify(req.headers));
返回JSON格式的列表。
{
"host":"localhost:8081",
"connection":"keep-alive",
"cache-control":"max-age=0",
"accept":"text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8",
"upgrade-insecure-requests":"1",
"user-agent":"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.107 Safari/537.36",
"accept-encoding":"gzip, deflate, sdch",
"accept-language":"en-US,en;q=0.8,et;q=0.6"
}