How to fetch from api routes in frontend?
Unanswered
Dutch posted this in #help-forum
DutchOP
in my frontend if i try to fetch from my backend by doing:
Nothing works and it says "/api" doesnt exist, however if i hardcode the localhost:3000 like this:
Everything works fine.
How can i avoid hardcoding the entire api url to my nextjs backend api?
const response = await fetch(`/api/user/${id}`);Nothing works and it says "/api" doesnt exist, however if i hardcode the localhost:3000 like this:
const response = await fetch(`http://localhost:3000/api/user/${id}`);Everything works fine.
How can i avoid hardcoding the entire api url to my nextjs backend api?
2 Replies
@Ray https://nextjs-faq.com/fetch-api-in-rsc
DutchOP
thank you kind sir, will read this