I'm looking a way to replicate the jQuery method getJSON but server side. The server I'm using is a node.js server with express framework written in coffeescript.
The code I have client side is :
# To get the client IP
$.getJSON("http://jsonip.com?callback=?", (data) ->
# To get more information about that IP
$.getJSON("http://freegeoip.net/json/" + data.ip, (fulldata) ->
console.log fulldata))
The fulldata variable give me information about the client IP.
I have to avoid using JavaScript client side, so I try to do the same server side, I get the client IP doing :
(req, res) ->
# To get the client IP
req.ip
But after that, I have no idea how to get the fulldata in json from the freegeoip.net server.
Help, anyone ?
I used Skelly solution.
So I did :
request = require 'request'
(...)
(req, res) ->
url = 'http://freegeoip.net/json/' + req.ip
request.get(url, (error, response, body) ->
if !error
console.log body
)
The body contains the data I need.
I'm sure the David Fregoli's native node.js solution works also, but the Request package works perfectly and easily.
Thanks to them both.
I'm unfamiliar with coffeescript, however, one of Node's default libraries called http (most often used to set up a server) can make http requests
var request = http.request({host: 'jsonip.com', port: 80, path: '?callback=?' , method: 'GET'}, function(res){
res.on('data', function (chunk) {
console.log('BODY: ' + chunk);
});
});