javascript 如何在 Meteor 中访问客户端 IP 地址?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18507998/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 12:12:26  来源:igfitidea点击:

How to access client IP address in Meteor?

javascriptnode.jsmeteormeteorite

提问by Chet

This seems like a very basic question that doesn't have an elegant solution/answer out there.

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

How can I access the client (remote) IP address from (1) the server or (2) the client?

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

采纳答案by Andrew Mao

As Florin mentioned, this is all pretty much integrated with Meteor now, as opposed to the dark ages when we had to do it ourselves. However, I've additionally wrapped it in a package that tracks all open connections and allows you to query for their IPs: https://github.com/mizzao/meteor-user-status. It also does a bunch of other useful stuff.

正如 Florin 所提到的,这一切现在几乎都与 Meteor 集成在一起,而不是我们不得不自己做的黑暗时代。但是,我另外将它包装在一个跟踪所有打开连接的包中,并允许您查询它们的 IP:https: //github.com/mizzao/meteor-user-status。它也做了一堆其他有用的东西。

回答by Florin Dobre

Getting the client IP:

获取客户端IP:

Without a http request, in the functions you should be able to get the clientIP with:

在没有 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

With a http request and using iron-router and its Router.map function:

使用 http 请求并使用 Iron-router 及其 Router.map 功能:

In the action function of the targeted route use:

在目标路线的动作功能中使用:

clientIp = this.request.connection.remoteAddress;

回答by hakan

On client

在客户端

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;
    }
});

On server:

在服务器上:

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;
    },
});

回答by Stephan Tual

You can use this package: https://github.com/gadicohen/meteor-headers. It gets headers on both client and server.

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

If you want to do it without a package, you can 'inspire' yourself from the code above, the thing to remember is that prior to 0.6.5 we used the 'hidden' __meteor_bootstrap__.appand post 0.6.5 it's recommended to use WebApp.connectHandlerinstead.

如果你想在没有包的情况下做到这一点,你可以从上面的代码中“启发”自己,要记住的是,在 0.6.5 之前,我们使用了“隐藏”,__meteor_bootstrap__.app而在 0.6.5 之后,建议改用它WebApp.connectHandler