I'm developing a Next.js application and I need to connect to a backend server that is running on a different port. I'm having trouble figuring out how to establish this connection.
Currently, my Next.js application is running on port 3000, while the backend server is running on port 8000. I want to make API calls from my Next.js pages to the backend server to fetch data and perform CRUD operations.
I have tried using the fetch()
function in Next.js to make the API calls, but it seems to default to the same port (3000) as the frontend. I'm unsure how to configure it to connect to the backend server on port 8000.
Here's a simplified example of what I've tried:
fetch('/api/data') // This defaults to port 3000
.then(response => response.json())
.then(data => console.log(data))
.catch(error => console.error(error));
Could someone please guide me on how to establish a connection to a backend running on a different port (8000) in Next.js? Any help or suggestions would be greatly appreciated. Thank you!