How do I get the current domain and scheme in a route handler?
Answered
Ratonero Mallorquin posted this in #help-forum
Ratonero MallorquinOP
I can easily make a route handler:
And this gives me the full requested url. For example
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
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/testBut 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 // domain1 Reply
@Ratonero Mallorquin 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?
const url = new URL(req.url)
url.protocol // https
url.host // domainAnswer