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.

Share Improve this question edited Jun 19, 2022 at 9:56 Stephen Ostermiller 25.5k16 gold badges95 silver badges115 bronze badges asked Aug 28, 2013 at 21:41 Nicola PeluchettiNicola Peluchetti 76.9k32 gold badges149 silver badges195 bronze badges 4
  • 1 try: req.host or req.get('host') expresses docs – dc5 Commented Aug 28, 2013 at 21:45
  • 3 node.js: req.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
  • I get this warning: express deprecated req.host: Use req.hostname instead index.js:20:8 – Adam Fowler Commented Mar 8, 2015 at 3:15
  • "I need to know if the API was called by somesite.example". Note that the domain example.com is reserved specifically for use in examples: iana.org/domains/reserved – Stijn de Witt Commented Jul 1, 2022 at 7:45
Add a comment  | 

5 Answers 5

Reset to default 221

You 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