-2

I want to create a server-side app using node.js with express. At the moment I am able to get the json document with the location and temperature but after I used pykite to get it online the application would only get the server location to the one who is accessing it. Here is the code in its current form:

var express = require('express');
var app = express();
var request = require('request');



    app.get('/api/weather/current_temperature', function (req, res) {
        request('http://api.wunderground.com/api/ebb3e0cf5059cce8/conditions/q/autoip.json', function(error, response, body){
            if (!error && response.statusCode==200){
              res.setHeader('Content-Type', 'aplication/json');    
              var result = JSON.parse(body);
              res.json({ "location": result.current_observation.observation_location.city,"temperature": result.current_observation.temp_c });
            }else{
                console.log(error, response.statusCode, body);
            }
            res.end("")
        });
    });

    var server = app.listen(3000, function () {
    var host = server.address().address;
    var port = server.address().port;
        console.log('The app is up.');
    });

I admit I'm new to this but I wanna learn. I would like some help implementing it in this code, if it's possible, a way to access the user's location and determine the weather at his/her location. Thank you in advance.

P.S.: I would also like some links into some documentation, don't wanna skip anything.

Cypher
  • 65
  • 11
  • A Simple Google search would give you results. http://stackoverflow.com/questions/27234861/correct-way-of-getting-clients-ip-addresses-from-http-request-golang – Akshay Khandelwal Mar 01 '17 at 13:16

1 Answers1

0

The URL you are linking to http://api.wunderground.com/api/YOUR_KEY/conditions/q/autoip.json (which doesn't work because you didn't provide your key) has autoip in the name.

That implies that it will return a result from whatever IP address the request came from.

So you have two options:

  • Don't make the request from NodeJS. Make the request from the browser. (This probably isn't advisable given the use of an API key and might run into Same Origin Policy issues).
  • Find a different API (possibly one from the same site) that lets you specify what IP address or location to use.
Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335