Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to fetch an API in NextJS from the api folder

Tags:

next.js

I created an API with NextJS in the API folder. Now I want to fetch the data from that API in the getServerSideProps function.

const res = await fetch(`localhost:3000/api/playlist`);

But I get an error that says that it only supports HTTPS protocols.

Server Error

TypeError: Only HTTP(S) protocols are supported
This error happened while generating the page. Any console logs will be displayed in the terminal window.

How can I fetch the data from the NextJS API on my localhost? Thanks for you helping out!

like image 411
Holunderyogele Avatar asked Oct 27 '25 14:10

Holunderyogele


1 Answers

I found the problem, I forgot to add http:// in front of the URL.

The code would then look like this:

const res = await fetch(`http://localhost:3000/api/playlist`);
like image 126
Holunderyogele Avatar answered Oct 29 '25 05:10

Holunderyogele



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!