我有这个代码,当用户点击http://domain.tld/stats/1.gif并使用geoip-lite库在raphael地图上返回lat和long时,它基本上会查找IP地址。
var http = require('http'),
util = require('util'),
static = require('node-static'),
faye = require('faye'),
url = require('url'),
geoip = require('geoip-lite');
function LiveStats(options) {
if (! (this instanceof arguments.callee)) {
return new arguments.callee(arguments);
}
...
} else if (location.pathname == '/stats/1.gif' && request.method == 'GET') {
var origin;
response.writeHead(200, {
'Content-Type': 'image/gif'
});
origin = /\/(.*)\.gif/.exec(request.url);
if (origin) {
var ip = request.connection.remoteAddress;
var geo = geoip.lookup(ip);
console.log(geo);
self.bayeux.getClient().publish('/stat', {
title: 'user'
, latitude: geo.ll[0]
, longitude: geo.ll[1]
, ip: ip
});
}
response.end("OK");
} else {
file.serve(request, response);
}
});
});
return server;
};
module.exports = LiveStats;
的console.log(GEO);
返回例如:
{ range: [ 1391911936, 1391915007 ],
country: 'FR',
region: 'A9',
city: 'N?mes',
ll: [ 43.8333, 4.35 ] }
城市格式错误,我应该如何纠正,以便打印Nîmes
答案 0 :(得分:1)
我用geoip-lite
尝试了你的IP地址,正如你所说,8位字符似乎不起作用。我使用geoip运气很好,它不仅处理宽字符,而且似乎也有更高的分辨率。我建议你尝试一下,如果你不能解决这个问题。
更新:使用geoip
。首先,您必须download GeoLiteCity.dat
from Maxmind。
var City = require("geoip").City,
city = new City(__dirname + "/GeoLiteCity.dat");
city.lookup("82.246.239.255", function(err, location) {
console.log(location);
}
给我:
{ country_code: 'FR',
country_code3: 'FRA',
country_name: 'France',
region: 'A9',
city: 'Nîmes',
latitude: 43.83330154418945,
longitude: 4.349999904632568,
continent_code: 'EU' }
答案 1 :(得分:0)
如果我没记错的话,MaxMind的GeoIp数据库会以UTF-8为您提供结果。我怀疑控制台日志的读者虽然存在正确的字符但却没有正确解码。
你能用能识别UTF-8的东西打开那个JSON对象吗?