Corentin Caer Corentin Caer - 2 months ago 6
Node.js Question

Express get sub-domain name

is there a way to use the express router to get the variable of the subdomain.

Lets say I have

foo.example.com
how can I get an output of
foo
.

Thanks

Answer

In Express 4.x you can use req.subdomains property.

// Host: "tobi.ferrets.example.com"
req.subdomains
// => ["ferrets", "tobi"]

ref.: https://expressjs.com/en/4x/api.html#req.subdomains

Comments