Next.js Discord

Discord Forum

How do I get the current domain and scheme in a route handler?

Answered
Ratonero Mallorquin posted this in #help-forum
Open in Discord
Ratonero MallorquinOP
I can easily make a route handler:

export async function GET(req: Request) {
  return new Response("hello world on url: " req.url);
}


And this gives me the full requested url. For example http://localhost:3000/test

But how can I, in this context, get "only" the domain nextjs is serving this request over?

I can get the Host header from Request, but I can't get the scheme (http vs https).

Any idea how to get everything but the path? Is splitting req.url by regex the only solution here?
Answered by Ray
const url = new URL(req.url)
url.protocol // https
url.host // domain
View full answer

1 Reply