我有以下代码,这是我正在尝试构建的快速应用程序的一部分:
app.get('/stat/1.gif', function( req, res ) {
var time = +new Date();
var origin;
res.writeHead(200, {'Content-Type': 'image/gif'});
origin = /\/(.*)\.gif/.exec(req.url);
console.log(origin);
if (origin) {
var ip = req.ip;
if (ip === null || ip === "127.0.0.1") {
ip = "82.246.239.187";
}
location = geoip.lookup(ip);
console.log(location);
var obj;
console.log(location.city);
console.log(location.ll[1]);
console.log(location.ll[0]);
obj = {
city: location.city
,longitude: location.ll[1]
,latitude: location.ll[0]
,ip: ip
,timestamp: time
};
console.log(obj);
everyone.now.receiveLocation(obj);
} else {
console.log( 'fixme no origin' );
}
});
查看console.log()我得到的是obj
{ city: 'Nîmes',
longitude: 4.3596,
latitude: 43.8338,
ip: '82.246.239.187',
timestamp: 1359930392288 }
但我一直收到这个错误:
TypeError: Property 'receiveLocation' of object #<Object> is not a function
at /Users/khinester/Documents/Tutorials/Node/Blade/server.js:188:18
at callbacks (/Users/khinester/Documents/Tutorials/Node/Blade/node_modules/express/lib/router/index.js:161:37)
at param (/Users/khinester/Documents/Tutorials/Node/Blade/node_modules/express/lib/router/index.js:135:11)
at pass (/Users/khinester/Documents/Tutorials/Node/Blade/node_modules/express/lib/router/index.js:142:5)
at Router._dispatch (/Users/khinester/Documents/Tutorials/Node/Blade/node_modules/express/lib/router/index.js:170:5)
at Object.router (/Users/khinester/Documents/Tutorials/Node/Blade/node_modules/express/lib/router/index.js:33:10)
at next (/Users/khinester/Documents/Tutorials/Node/Blade/node_modules/express/node_modules/connect/lib/proto.js:199:15)
at resume (/Users/khinester/Documents/Tutorials/Node/Blade/node_modules/express/node_modules/connect/lib/middleware/static.js:60:7)
at SendStream.error (/Users/khinester/Documents/Tutorials/Node/Blade/node_modules/express/node_modules/connect/lib/middleware/static.js:73:37)
at SendStream.EventEmitter.emit (events.js:115:20)
我正在使用https://github.com/bluesmoon/node-geoip
我使用https://github.com/kuno/GeoIP时使用了以下代码:
app.get('/stat/1.gif', function( req, res, next ) {
var time = +new Date();
var origin;
res.writeHead(200, {'Content-Type': 'image/gif'});
origin = /\/(.*)\.gif/.exec(req.url);
if (origin) {
var ip = req.headers['x-real-ip'];
if (ip === null || ip === "127.0.0.1") {
ip = "82.246.239.187";
}
city.lookup(ip, function(err, location) {
var obj;
if ( !err && location ) {
obj = {
city: location.city
,longitude: location.longitude
,latitude: location.latitude
,ip: ip
,timestamp: time
};
} else {
console.log( 'server fake location' );
obj ={
city: 'Bexleyheath',
longitude: 0.15000000596046448,
latitude: 51.45000076293945,
ip: '86.173.61.119',
timestamp: 1343054092459
};
}
everyone.now.receiveLocation(obj);
console.log(obj);
});
console.log(origin[1], req.connection.remoteAddress, req.headers['user-agent']);
} else {
console.log( 'fixme no origin' );
}
});
这个工作正常,但我需要使用geoip-lite作为我想要热的主机,这里没有geoip c库。
任何建议都非常感激。