NEED HELP with API project request header parser microservice

NEED HELP with API project request header parser microservice
0.0 0

#1

I have been following similar procedures taught by this youtuber from freeCodeCamp: https://www.youtube.com/playlist?list=PLtwj5TTsiP7vmgyFgrwtPxybl9KzMvmAB

I first use Mac terminal and sublime text 3 to use npm and code, then I deploy to heroku.

var express = require("express");
var router = express.Router();
var requestIP = require("request-ip")
/* GET home page. */
router.get('/', function(req, res) {
  var language = req.headers['accept-language'].split(",")[0]
  var software = req.headers['user-agent'].match(/\((.+?)\)/)[1]
  var ip = requestIP.getClientIp(req)
  var result = {'ipaddress' : req.ip ,'Language' : language, 'software': software };
  res.json(result);
  console.log(JSON.stringify(req.headers));
});
module.exports = router;

I was able to extract software detail and language property from the request header, but I just couldn’t extract the IP address at all.
Both req.ip and request-ip module and its method doesnt return a correctly formatted ip-address. It shows something like this:
“ipaddress”:"::ffff:10.178.64.67"

Please advise, any kind souls?

This is the url of my project in heroku: https://dariusheaderparser.herokuapp.com/
This is the github repo of my project: https://github.com/hesingon/FreeCodeCamp-header-parser


#2

Hello

The IP address returned is actually the equivalent IPv4 address converted to an IPv6 address. IPv6 comes into play because we ran out of numbers. :slight_smile:

Please see this more informative response from Nick (Stackoverflow)

In my opinion, you can display either ::ffff:<IPv4 address> or just the <IPv4 address>


#3

Hi Russell, thank you so much for the response. Another question: the IPv4 address number returned by the program I wrote is different from the sample programme given in the project description page: https://cryptic-ridge-9197.herokuapp.com/api/whoami/

Any idea why this is so?


#4

Interesting. I just checked my own solution and it behaves the same way meaning that I’ll have to change it. Thanks :slight_smile:

The 10...* address range is reserved for private networks, so Heroku is doing some internal routing.

Have a look at the X-Forwarded-For header and see if you can find the correct address from there.


#5

Also, see https://stackoverflow.com/questions/10849687/express-js-how-to-get-remote-client-address for Express specific information.