Fetching from own api route
See original GitHub issueIn Next.js if you had an api endpoint at ‘/pages/api/joke/’ inside of a page you can call:
const resp = await fetch('/api/joke')
and it will fetch the data that endpoint returns.
Is there already a simple way to do this in fresh? Because so far I have had to do this weird workaround with http request headers:
//routes/test.tsx
//Fetches from the example "joke" api endpoint\
//Inside GET in handler:
const url = 'http://' + req.headers.get('host')?.toString() + '/api/joke';
const resp = await fetch(url);
Is there a built in way to get the url of the project so you can easily call your own api endpoints within the app? (like the Next.js example)
Currently I have been working with a simple utility I made that basically just returns the url given a Request object, but if there was a baked in way this would be much easier
Issue Analytics
- State:
- Created a year ago
- Reactions:2
- Comments:5 (1 by maintainers)
Top Results From Across the Web
API Routes Details - Next.js
API Routes Details. Here is some essential information you should know about API Routes. Do Not Fetch an API Route from getStaticProps or...
Read more >Internal API fetch with getServerSideProps? (Next.js)
It can be tempting to reach for an API Route when you want to fetch data from the server, then call that API...
Read more >Fetch data from an API on the server-side with ... - Egghead.io
In this lesson, we will learn how to use getServerSideProps to dynamically fetch data and server-side render pages by consuming a "user profile"...
Read more >Understanding how API routes work in Next.js
Let's break this code down - for an API route to work you need to export a function, that receives two parameters: request...
Read more >API Routes for Next.js - Topcoder
API routing works similarly to page-based routing. The filename of an API is used to associate it with a route. Every API route...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
In the latest version, you can simply call the handler function defined in the API routes.
This is the joke.ts file comes with the example:
You can simply access this API route by using it like;
@WittySmirk did you use
IS_BROWSER
should work for client sideit happen to my when I fetch some data in
Nextjs
in server side you should bring absolute url