What is your hint or solution suggestion?
This is a simple project that wants you to process the information provided by the computer to the browser and send it back
sample output:
{
"ipaddress": "42.193.243.32",
"language": "en-US,en;q=0.9",
"software": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.105 Safari/537.36"
}
step 1:
making an API endpoint
app.get('api/whoami', (req,res) => {...})
step 2:
getting the IP address.
when you use req.header, it will return an obj with data about the client
the raw output will look something like this
"42.182.219.253,::ffff:10.10.10.51,::ffff:10.10.80.165"
in order to get the IP that meets the requirements, you need to process it with the code below
let ip = req.headers["x-forwarded-for"].split(",")[0]
this piece of code gets the IP, and then extracts the first element
step 3:
getting the language
the req.headers also provides data about the languages that is accepted by the client
code below gets the language
let lang = req.headers["accept-language"]
step 4:
getting the user-agent
code below will return the user agent (browser info and OS)
let software = req.headers["user-agent"]
step 5:
send the data out as JSON
res.json({ipaddress: ip, language: lang, software: software}
full code:
app.get('/api/whoami', (req,res) => {
let ip = req.headers["x-forwarded-for"].split(",")[0]
let lang = req.headers["accept-language"]
let software = req.headers["user-agent"]
res.json({ipaddress: ip, language: lang, software : software, x: req.headers['x-forwarded-for']})
})
===============================================================
Challenge: Request Header Parser Microservice
Link to the challenge: