0

I am trying to figure out what the best way would be to pass geoIP info of users to client-side javascript. I have configured nginx for this and am able to send the info to my node + express server.

I don't know what the next step is. From googling around i can see the headers I am trying to send can't directly be read by js on client-side.

This is what I have --

Nodejs--

router.get('/', function(req, res, next) {
  res.setHeader("geoip_country_code", req.headers.geoip_country_code);
  res.setHeader("geoip_city", req.headers.geoip_city);
  res.render('index', { title: 'bla' });
  console.log(req.headers);
});

Nginx --

location / {
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header GEOIP_COUNTRY_CODE $geoip_country_code;
            proxy_set_header GEOIP_CITY $geoip_city;
            proxy_set_header GEOIP_LATITUDE $geoip_latitude;
            proxy_set_header GEOIP_LONGITUDE $geoip_longitude;
            proxy_pass http://app:3000;
        }
charsi
  • 2,917
  • 22
  • 40

2 Answers2

2

After a bit more google-fu i have answered my own question.

res.locals is the best way to do this in Express.

So on the server now I am doing --

router.get('/', function(req, res, next) {
  res.locals.geoip_country_code = req.headers.geoip_country_code;
  res.locals.geoip_city = req.headers.geoip_city;
  res.render('index', { title: 'bla' });
  console.log(req.headers);
});

And in my Jade template I have --

script(type='text/javascript').
    var geoip_city =!{JSON.stringify(geoip_city)}
    var geoip_country_code = !{JSON.stringify(geoip_country_code)}

source : so/10919650/ (Bit intimidating that this was answered almost 5 years ago!)

Community
  • 1
  • 1
charsi
  • 2,917
  • 22
  • 40
0

You can do it use nginx add_header:

location / {
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header GEOIP_COUNTRY_CODE $geoip_country_code;
            proxy_set_header GEOIP_CITY $geoip_city;
            proxy_set_header GEOIP_LATITUDE $geoip_latitude;
            proxy_set_header GEOIP_LONGITUDE $geoip_longitude;

            add_header geoip_country_code "$geoip_country_code" always;
            add_header geoip_city "$geoip_city" always;

            proxy_pass http://app:3000;
        }
stdob--
  • 28,222
  • 5
  • 58
  • 73
  • How is this different from `proxy_set_header` that I am already using? Would I be able to access them from client js this way? – charsi Feb 22 '17 at 23:40
  • 2
    `proxy_set_header` add header from client to server, but `add_header` add header from server to client. – stdob-- Feb 22 '17 at 23:41
  • Ah okay I can see I am doing this the wrong way around then. Can the nginx headers be queried from client js? – charsi Feb 22 '17 at 23:43
  • @charsi Sorry, I misunderstood your question, you do not need to send headers to the client side, but pass them in the client javascript. In this case is suitable your version of the solution. – stdob-- Feb 22 '17 at 23:48