0

i am working on a project in NODE.JS in which when user logged in,his area's weather is automatically shown on his dashboard. According to my understanding,when a client comes:

1)His ip is traced for which m using request-ip.

2)Then according to this ip dimensions of his area can be determined.for this m using satelize.

3)Then on the basis of longitude and latitude,his area's weather can be predicted with the help of forcast.io.

problem is sending request to that middleware,m getting loopback ip.(localhost:3000).while i want to get any random client's ip.so that i can check whether my api is working correct or not. here is my code:

router.get('/',function(req,res){

  var clientIp = requestIp.getClientIp(req); //inside middleware handle

  satelize.satelize({ip:clientIp}, function(err, geoData) { // if data is JSON, we may wrap it in js object 

    var obj = JSON.parse(geoData);

    forecast.get([obj.latitude, obj.longitude], function(err, weather) { 

      if(err) return console.dir(err); 

      console.log(weather);

    });

  }); 

});
Jelle Kralt
  • 980
  • 6
  • 16
N.A
  • 855
  • 4
  • 13
  • 34
  • show some code with what have you tried – micnic Dec 30 '14 at 15:10
  • router.get('/',function(req,res){ var clientIp = requestIp.getClientIp(req); //inside middleware handle satelize.satelize({ip:clientIp}, function(err, geoData) { // if data is JSON, we may wrap it in js object var obj = JSON.parse(geoData); forecast.get([obj.latitude, obj.longitude], function(err, weather) { if(err) return console.dir(err); console.log(weather); }); }); }); – N.A Dec 31 '14 at 12:34
  • please edit your question with the content of the comment you wrote – micnic Jan 01 '15 at 18:03

1 Answers1

0

If I understand correctly, you'd like to use a random ip address when developing on your local machine. In order to do this, you'd have to set the clientIp variable based on your environment. You can use NODE_ENV for this, see https://stackoverflow.com/a/16979503/2494649 for a nice explanation.

If you want to use an ip address of a random client, you will have to collect these yourself and set it as the clientIp. You could for instance set an array of random client ip's and use them randomly.

var randomIps = ['1.2.3.4', '5.6.7.8', '123.123.123.123'];
var clientIp;

if(process.env.NODE_ENV === 'development') {
    // Select random IP address
  clientIp = randomIps[Math.floor(Math.random()*randomIps.length)];
} else {
  clientIp = requestIp.getClientIp(req); //inside middleware handle
}

You could use your app to save your client ip addresses to a datastore (anonymously) and use these addresses to provide a random ip address to the clientIp variable.

Community
  • 1
  • 1
Jelle Kralt
  • 980
  • 6
  • 16