The goal for this one is to get return to the user the IP address, language, and operating system of the browser/user making the request.
Code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
// whoami.js
// FreeCodeCamp // Backend Challenge 2 - Get requesting client IP Address
exports.who = function (req, res) {
var resData = {
ipaddress: null,
language: null,
software: null
}
resData.ipaddress = req.ip
if (req.header('accept-language')) {
resData.language = req.header('accept-language').split(',')[0]
}
if (req.header('user-agent')) {
var userAgent = req.header('user-agent')
var lParenIndex = userAgent.indexOf('(')
var rParenIndex = userAgent.indexOf(')')
if (lParenIndex > -1 && rParenIndex > -1) {
resData.software = userAgent.substr(lParenIndex+1, rParenIndex-lParenIndex)
}
}
res.json(resData)
}
|
Demo
View this code live on Heroku at fcc-challenges.herokuapp.com/api/whoami