Wednesday, 15 March 2017

How to get the current ip address by using koaJS

I want to get the current user ip address, and then call an external API to geo-locate the IP address, and then store the response data about geolocation in the request (such as latitude, longitude, etc), then call external API to get the specific data based on the geolocation information we got, store the new response data in the request, and call the next middleware. Now, I really don't know how to do these with koaJS, I tried to get the user ip address with this following way.

let koa = require('koa');
let app = new koa();

app.proxy = true;


app.use(function *(){
    this.body = this.req.ip; // also tried request.headers['x-forwarded-for'];
});

app.listen(3000, function(){
    console.log('Server running on https://localhost:3000')
});

But I can't get the ip address, can anyone tell me how to change my code, and any example for these requirements would be really helpful



via D.Kenny

No comments:

Post a Comment