admin管理员组文章数量:1134246
I'm using express.js and I need to know the domain which is originating the call. This is the simple code
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.example
or someothersite.example
.
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.
I'm using express.js and I need to know the domain which is originating the call. This is the simple code
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.example
or someothersite.example
.
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.
5 Answers
Reset to default 221You have to retrieve it from the HOST
header.
const 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.
If this is for supporting cross-origin requests, you would instead use the Origin
header.
const origin = req.get('origin');
Note that some cross-origin requests require validation through a "preflight" request:
req.options('/route', function (req, res) {
const origin = req.get('origin');
// ...
});
If you're looking for the client's IP, you can retrieve that with:
const userIP = req.socket.remoteAddress;
message.socket
.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.
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;
In Express 4.x you can use req.hostname
, which returns the domain name, without port. i.e.:
// Host: "example.com:3000"
req.hostname
// => "example.com"
See: http://expressjs.com/en/4x/api.html#req.hostname
req.get('host')
is now deprecated, using it will give Undefined
.
Use,
req.header('Origin');
req.header('Host');
// this method can be used to access other request headers like, 'Referer', 'User-Agent' etc.
Year 2022, I use express v4.17.1 get following result
var host = req.get('host'); // works, localhost:3000
var host = req.headers.host; // works, localhost:3000
var host = req.hostname; // works, localhost
var origin = req.get('origin'); // not work, undefined
var origin = req.headers.origin; // not work, undefined
本文标签: javascriptHow do I get the domain originating the request in expressjsStack Overflow
版权声明:本文标题:javascript - How do I get the domain originating the request in express.js? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736807987a1953771.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
req.host
orreq.get('host')
expresses docs – dc5 Commented Aug 28, 2013 at 21:45req.headers["x-forwarded-for"] || req.connection.remoteAddress
x-forwarded-for would cover your bases behind a proxy, load balancer... – Eat at Joes Commented May 16, 2014 at 19:13