以下是我的代码。
var myHttp = require("http");
var url = require("url");
var qString = require("querystring");
var fs = require('fs');
var myEvents = require('./customEvents');
var myAppWebServer = myHttp.createServer(function(request, response){
response.writeHead(405, {'content-type':'text/html'});
console.log(request.headers);
response.end('{ "name":"my Name", "location":"xxx"}');
});
myAppWebServer.listen(8080);
当我使用console.log(request.headers)打印响应头文本时,它会打印以下数据。
{
host: 'localhost:8080',
connection: 'keep-alive',
'x-os-version': 'Windows 7',
'user-agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.86 Safari/537.36',
'content-type': 'application/json',
location: 'bangalore',
'x-useragent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.86 Safari/537.36',
'x-enteringpage': 'http://localhost:8080/login',
accept: '*/*',
referer: 'http://localhost:8080/login',
'accept-encoding': 'gzip, deflate, br',
'accept-language': 'en-US,en;q=0.8',
cookie: '_ga=GA1.1.1554321962.1498024434; _gid=GA1.1.1406177709.1501568327'
}
我不知道如何在我的控制台中打印'x-useragent'。我使用下面的语法,但抛出错误。
console.log(request.headers.x-useragent);
有人可以提供帮助。
答案 0 :(得分:0)
具有特殊字符的Javascript对象键必须使用这种方式:
request.headers["x-useragent"]
You can access object properties in two ways:
objectName.propertyName
或
对象名[" propertyName的"]
使用chrome devtool snapshot编辑。