Replies: 1 comment 3 replies
-
|
Are you trying to call the server from within the server? That's not gonna fly (with relative paths), not even in pages directory. You need an absolute URL when invoking fetch, otherwise I think the only way to call the API route with relative paths is from the "outside" like, from the browser, or any other HTTP client. If you are not convinced then change to: const testFetch = await fetch('http://localhost:3000/api/testroute', {
method: 'GET',
});
console.log('testFetch: ', testFetch);What this should tell you is that you are making an unnecessary jump from the server, onto the internet (not quite on this case), and back to the server again. If you are within a server component, just fetch the data you need there. The point with these API handlers is to provide data to your client, at runtime, or other "consumers", which may not always want to have HTML for example. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
The docs give lots of of examples of how to establish and create new handlers but I haven't quite got my head around the logic of requesting them.
Are they meant to be explicitly requested?
For example, if I create the following route.js at /app/api/testroute/route.js
And then in /app/page.jsx :
I get a TypeError of:
TypeError: Failed to parse URL from /api/testrouteAm I misunderstanding how to use route.js, have I discovered a bug, or do I likely have a configuration problem?
I've pulled the latest canary image (13.1.7, I believe)
Beta Was this translation helpful? Give feedback.
All reactions