How to get the domain originating the request in Express.js?

Sometimes, we want to get the domain originating the request in Express.js.

In this article, we’ll look at how to get the domain originating the request in Express.js.

How to get the domain originating the request in Express.js?

To get the domain originating the request in Express.js, we can use req.get with the 'host' or 'origin' header string.

For instance, we write

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

to call req.get with 'host' to get the hostname of the client that made the request.

For getting the host of cross domain requests, we write

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

to get the hostname of a cross origin request by calling req.get with 'origin'.

Conclusion

To get the domain originating the request in Express.js, we can use req.get with the 'host' or 'origin' header string.