简体   繁体   English

从流星中的请求对象获取IP地址

[英]Get IP address from request object in Meteor

I want to retrieve the client's IP address before I redirect the client to an external URL. 在将客户端重定向到外部URL之前,我想检索客户端的IP地址。

Here is my code: 这是我的代码:

Router.route('/:_id', {name: 'urlRedirect', where: 'server'}).get(function () {
    var url = Urls.findOne(this.params._id);
    if (url) {
        var request = this.request;
        var response = this.response;
        var headers = request.headers;
        console.log(headers['user-agent']);
        this.response.writeHead(302, {
            'Location': url.url
        });
        this.response.end();
    } else {
        this.redirect('/');
    }
});

My question is: Can I get the IP address from the request object? 我的问题是:我可以从request对象获取IP地址吗?

According to the Iron-Router guide , the request you get from this.request will be a typical NodeJS request object: 根据Iron-Router指南 ,从this.request收到的请求将是典型的this.request请求对象:

Router.route('/download/:file', function () {
  // NodeJS request object
  var request = this.request;

  // NodeJS  response object
  var response = this.response;

  this.response.end('file download content\n');
}, {where: 'server'});

So you should be able to loop the IP address up in this.request.connection.remoteAddress . 因此,您应该可以在this.request.connection.remoteAddress循环IP地址。

As stated in this answer , you may also have to look in this.request.headers['x-forwarded-for'] in some cases. 该答案所述 ,在某些情况下,您可能还需要查看this.request.headers['x-forwarded-for']

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM