0
我對NodeJS和它的所有迷人有點新鮮。我試圖弄清楚如何結合geoip-lite和request-ip npm軟件包來獲取我的應用的大致用戶位置。Nodejs客戶端IP
到目前爲止,我有這個
var requestIp = require('request-ip');
var geoip = require('geoip-lite');
var express = require('express');
var app = express();
//var ip = "108.219.41.178";//207.97.227.239
//var ip = req.headers['x-real-ip'] || req.connection.remoteAddress;
//var ip = req.header('x-forwarded-for') || req.connection.remoteAddress;
var ip = ???
var geo = geoip.lookup(ip);
var port = process.env.PORT || 3000;
/*app.use('/', function(req, res, next) {
var ipMiddleware = function(req, res, next) {
var clientIp = requestIp.getClientIp(req);
next();
};
});*/
app.get('/', function(req, res) {
res.send('<html><head><head/><body><h1>Hello World!</h1></body></html');
});
app.listen(port, function() {
console.log('Hello on 3000');/*always put console.log to see if its running/working*/
console.log(geo);
console.log("The IP is %s", geoip.pretty(ip));
});
/*node app.js to run*/
// var ip = req.headers['x-real-ip'] || req.connection.remoteAddress;
我無法找出使用請求IP來獲得IP。 geoip-lite非常容易,花了2分鐘。我想我對Nodejs的noobness也沒有幫助。一個代碼示例,告訴我我在做什麼錯誤或沒有想到會很好。
你用什麼URL來測試?是偶然的,就像'http:// localhost:3000 /'? – Sgnl