Skip to content

Correct way to get request host? #3644

Closed Answered by alexgleason
alexgleason asked this question in Q&A
Discussion options

You must be logged in to vote

According to the fetch spec, req.headers:

Returns a Headers object consisting of the headers associated with request. Note that headers added in the network layer by the user agent will not be accounted for in this object, e.g., the "Host" header.

Therefore the correct way is to get the host with new URL(req.url).host.

You just need to be sure if you have any reverse-proxy, it's sending the Host header down to your runtime. Using Nginx with Deno, the host is available in the request URL.

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by alexgleason
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
1 participant