Doubts regarding using nextjs API router if am using nestjs for my backend
Unanswered
Havanese posted this in #help-forum
HavaneseOP
Hello guys, I am new to next.js and dont know its best practices.
I have my backend hosted on nestJS. How should I call my backend from my nextjs app?
Should I call the backend directly using axios or should I use the API router for calling my backend?
I have my backend hosted on nestJS. How should I call my backend from my nextjs app?
Should I call the backend directly using axios or should I use the API router for calling my backend?
10 Replies
You can think about Next as a backend for fronted
say your Nest backend provides functions to get users, products, payment
a Next.js API endpoint could be dedicated to aggregate all the products a user bought with payment info, to show those infos on some specific page, by calling the Nest.js generic CRUD endpoint
here is the original paper that coined the term
you'll notice the author designed this pattern for mobile initially and was skeptical about using it in the web, but it turns out this how you write web backends now in Next
note that with React Server Components, you don't even need an API endpoints
you can just fetch and aggregate the data from thec omponent
the same holds for Server Actions : for mutations (create/update/delete), you "just" call the a function, a server action, instead of crafting a whole POST endpoints
API Routes/API endpoints are necessary only if you need to call them from client-side code (which may happen a lot, that's not a problem at all, but not the default in Next which try to rely on server actions/RSCs as much as possible)