如何访问Meteor的客户端IP地址?

这似乎是一个非常基本的问题,没有一个优雅的解决scheme/答案在那里。

如何从(1)服务器或(2)客户端访问客户端(远程)IP地址?

正如弗洛林提到的,现在,这几乎与meteor相结合,而不是黑暗的时代,而我们必须自己做。 不过,我另外将它封装在一个包中,可以跟踪所有打开的连接,并允许您查询他们的IP: https : //github.com/mizzao/meteor-user-status 。 它也做了一堆其他有用的东西。

获取客户端IP:

没有http请求,在函数中你应该能够得到clientIP:

clientIP = this.connection.clientAddress; //EX: you declare a submitForm function with Meteor.methods and //you call it from the client with Meteor.call(). //In submitForm function you will have access to the client address as above 

使用http请求并使用铁路由器和它的Router.map函数:

在目标路线的行动function中使用:

 clientIp = this.request.connection.remoteAddress; 

在客户端

 headers = { list: {}, get: function(header, callback) { return header ? this.list[header] : this.list; } } Meteor.call('getReqHeaders', function(error, result) { if (error) { console.log(error); } else { headers.list = result; } }); 

在服务器上:

 headers = { list: {}, get: function(header) { return header ? this.list[header] : this.list; } }; var app = typeof WebApp != 'undefined' ? WebApp.connectHandlers : __meteor_bootstrap__.app; app.use(function(req, res, next) { reqHeaders = req.headers; return next(); }); Meteor.methods({ 'getReqHeader': function(header) { return reqHeaders[header]; }, 'getReqHeaders': function () { return reqHeaders; }, }); 

你可以使用这个包: https : //github.com/gadicohen/meteor-headers 。 它在客户端和服务器上都获得标题。

如果你想在没有包的情况下做,你可以从上面的代码中“启发”自己,要记住的是,在0.6.5之前,我们使用了'hidden' __meteor_bootstrap__.app并且在0.6.5之后推荐使用而不是WebApp.connectHandler