Below is my code.
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);
When i print response header text using console.log(request.headers) it prints the below data.
{
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'
}
I am not sure how to print 'x-useragent' in my console. I used below syntax but throws error.
console.log(request.headers.x-useragent);
Can somebody help.
Javascript object key with special character have to use this way:
request.headers["x-useragent"]
You can access object properties in two ways:
objectName.propertyName
or
objectName["propertyName"]
edited with chrome devtool snapshot.