Next.js Discord

Discord Forum

how to fetch data from /app/api

Unanswered
Sweat bee posted this in #help-forum
Open in Discord
Sweat beeOP
hey, I am trying to fetch data from /app/api from the server side?! Is this not possible? I simply use fetch('/api/users') but this doesnt work

11 Replies

Cape horse mackerel
I think for the server side fetching you should do
- fetch("/api/users");
+ fetch("http://localhost:3000/api/users");
but this doesnt work

http://www.catb.org/~esr/faqs/smart-questions.html#bespecific
"Stupid:
HELP! Video doesn't work properly on my laptop!"

Please ask a smart question
Sweat beeOP
ok and when deployed to vercel? I dont know on what port the node server is running there...
wait than it should just be the url where the app is running on... I gonna check that out
@Sweat bee hey, I am trying to fetch data from /app/api from the server side?! Is this not possible? I simply use fetch('/api/users') but this doesnt work
also, to fetch from /api/users, you should have /app/api/users/route.ts (or something similar) - but as the link above saysm you shouldn't fetch your own api in server side
@Sweat bee hey, I am trying to fetch data from /app/api from the server side?! Is this not possible? I simply use fetch('/api/users') but this doesnt work
White-winged Scoter
There is absolutely no point to fetching server side api routes from the server.
That's why 'require' and 'import' exists
Just create a function under that route that does what you need it to
then require or import it
Sweat beeOP
the reason I am doing this is to simulate real data fetches which are behind a vpn and has no access to from the staging system... So I thought I create some mock files and simulate those data fetches by adding route handlers and then just change environment vars from the official server host to the one the staging is running on... if this does make sense?! But yes I could also just import the files directly, check in which environment I am and use mock if staging and the official server api if on prod