help icon indicating copy to clipboard operation
help copied to clipboard

How do I get the domain originating the request in express.js?

Open saurav2501 opened this issue 9 years ago • 6 comments

  • Node.js Version: v6.10.2
  • OS: windows10

How do I get the domain originating the request in express.js? If I'm hitting from server2 to get server1 API data then I want to get details of server2 (i.e from which domain request is getting generated).

saurav2501 avatar Nov 20 '17 09:11 saurav2501

http://expressjs.com/en/4x/api.html is where express APIs are explained. Please see if req.ip helps you.

gireeshpunathil avatar Nov 22 '17 06:11 gireeshpunathil

@gireeshpunathil has pointed you to the right place for current Express API docs, however, after a brief review of APIs there, you'll perhaps note the Express leverages the built-in 'http' module. As such, I think what you're asking regarding the server name is really a call to the underlying 'http' module APIs, unless you want to process the IP address as Gireesh directed? In that respect, perhaps what you want (servername) is found in the result of:

req.get('host')

shellberg avatar Nov 22 '17 12:11 shellberg

ping @saurav2501

gireeshpunathil avatar Mar 07 '18 03:03 gireeshpunathil

closing as answered, please re-open if it is still not working.

gireeshpunathil avatar Mar 09 '18 05:03 gireeshpunathil

req.get('host') shows me backend instead of name of my virtual host.

alex700 avatar Jan 09 '20 11:01 alex700

use req.hostname for getting client side hostname if using express.js

Elanza-48 avatar May 16 '20 06:05 Elanza-48