如何在express.js中获取发起请求的域?

时间:2021-08-23 02:33:17

I'm using express.js and i need to know the domain which is originating the call. This is the simple code

我用快递。js和我需要知道发起调用的域。这是简单的代码

app.get(
    '/verify_license_key.json',
    function( req, res ) {
        // do something

How do i get the domain from the req or the res object? I mean i need to know if the api was called by somesite.com or someothersite.com. I tried doing a console.dir of both req and res but i got no idea from there, also read the documentation but it gave me no help.

如何从req或res对象获取域?我的意思是,我需要知道这个api是否被somesite.com或someothersite.com所调用。我试着做一个控制台。req和res的导演,但我不知道从那里,我也读了文件,但它没有帮助我。

2 个解决方案

#1


70  

You have to retrieve it from the HOST header.

您必须从主机头中检索它。

var host = req.get('host');

It is optional with HTTP 1.0, but required by 1.1. And, the app can always impose a requirement of its own.

它在HTTP 1.0中是可选的,但在1.1中是必需的。而且,这个应用程序总是可以强加自己的要求。


If this is for supporting cross-origin requests, you would instead use the Origin header.

如果这是为了支持跨源请求,您应该使用源头。

var origin = req.get('origin');

Note that some cross-origin requests require validation through a "preflight" request:

注意,一些跨产地来源的请求需要通过“飞行前”请求进行验证:

req.options('/route', function (req, res) {
    var origin = req.get('origin');
    // ...
});

If you're looking for the client's IP, you can retrieve that with:

如果您正在查找客户端的IP,您可以通过以下方式检索:

var userIP = req.socket.remoteAddress;

Note that, if your server is behind a proxy, this will likely give you the proxy's IP. Whether you can get the user's IP depends on what info the proxy passes along. But, it'll typically be in the headers as well.

注意,如果您的服务器位于代理之后,那么这可能会给您代理的IP。能否获得用户的IP取决于代理传递的信息。但是,它通常也在页眉中。

#2


8  

Instead of:

而不是:

var host = req.get('host');
var origin = req.get('origin');

you can also use:

您还可以使用:

var host = req.headers.host;
var origin = req.headers.origin;

#1


70  

You have to retrieve it from the HOST header.

您必须从主机头中检索它。

var host = req.get('host');

It is optional with HTTP 1.0, but required by 1.1. And, the app can always impose a requirement of its own.

它在HTTP 1.0中是可选的,但在1.1中是必需的。而且,这个应用程序总是可以强加自己的要求。


If this is for supporting cross-origin requests, you would instead use the Origin header.

如果这是为了支持跨源请求,您应该使用源头。

var origin = req.get('origin');

Note that some cross-origin requests require validation through a "preflight" request:

注意,一些跨产地来源的请求需要通过“飞行前”请求进行验证:

req.options('/route', function (req, res) {
    var origin = req.get('origin');
    // ...
});

If you're looking for the client's IP, you can retrieve that with:

如果您正在查找客户端的IP,您可以通过以下方式检索:

var userIP = req.socket.remoteAddress;

Note that, if your server is behind a proxy, this will likely give you the proxy's IP. Whether you can get the user's IP depends on what info the proxy passes along. But, it'll typically be in the headers as well.

注意,如果您的服务器位于代理之后,那么这可能会给您代理的IP。能否获得用户的IP取决于代理传递的信息。但是,它通常也在页眉中。

#2


8  

Instead of:

而不是:

var host = req.get('host');
var origin = req.get('origin');

you can also use:

您还可以使用:

var host = req.headers.host;
var origin = req.headers.origin;