如何在远程服务器上从远程服务器获取JSON?

我正在寻找一种方法来复制jQuery方法getJSON,但服务器端。 我使用的服务器是一个用coffeescript编写的express框架的node.js服务器。

我有客户端的代码是:

# 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)) 

fulldatavariables给我关于客户IP的信息。

我必须避免使用JavaScript客户端,所以我尝试做同样的服务器端,我得到的客户端IP:

 (req, res) -> # To get the client IP req.ip 

但之后,我不知道如何从freegeoip.net服务器获取json中的完整数据。

帮助,任何人?

我使用了Skelly解决scheme。

所以我做了 :

 request = require 'request' (...) (req, res) -> url = 'http://freegeoip.net/json/' + req.ip request.get(url, (error, response, body) -> if !error console.log body ) 

正文包含我需要的数据。

我相信David Fregoli的本地node.js解决scheme也可以工作,但是Request包可以完美和轻松地工作。

感谢他们两个。

我不熟悉coffeescript,但是,Node的一个默认库叫做http(最常用来设置服务器)可以发出http请求

 var request = http.request({host: 'jsonip.com', port: 80, path: '?callback=?' , method: 'GET'}, function(res){ res.on('data', function (chunk) { console.log('BODY: ' + chunk); }); });