Request Header Parser Microservice

FreeCodeCamp Backend Challenge 2

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

// 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