Next.js Discord

Discord Forum

About Next API route

Answered
tachtme posted this in #help-forum
Open in Discord
I'm just starting in nextjs but what I quite don't understand is what is the purpose of making API endpoints in nextjs project if we can just fetch the backend(nodejs or something) directly in our server components? can someone give me examples of use cases.
Answered by Philippine Crocodile
If you're going to do serverless stuff, having your API in NextJS would make that super easy.

Its also easier to put everything in NextJS, rather than making an entirely new ExpressJS backend.

As for examples of use cases, I don't know anything sites or projects I can point to as an example.
View full answer

22 Replies

Philippine Crocodile
If you're going to do serverless stuff, having your API in NextJS would make that super easy.

Its also easier to put everything in NextJS, rather than making an entirely new ExpressJS backend.

As for examples of use cases, I don't know anything sites or projects I can point to as an example.
Answer
Which means this flow is kinda redundancy? Nextjs front end > fetch to > nextjs backend api > fetch to > express js backend > communicate with database
and can be reduced to Nextjs front end > fetch to > nextjs backend api > communicate with database
@tachtme Which means this flow is kinda redundancy? Nextjs front end > fetch to > nextjs backend api > fetch to > express js backend > communicate with database
Philippine Crocodile
Yea. You could also do frontend fetches data from > expressjs backend
I see. so the NextJs API routes is just an optional to implement
which also means the nextjs can also become a front-end only
Philippine Crocodile
Yea. I'm pretty sure a lot of Next is optional. Like you don't have to use the <Link> component. You can just as easily use a normal <a> tag to navigate different pages
@tachtme which also means the nextjs can also become a front-end only
Philippine Crocodile
Yep
Philippine Crocodile
np
Asian black bear
Actually, public API routes do serve a purpose. If you want to expose certain details to 3rd party users or other SPAs and apps that don't run directly on your server you'd use API router as usual.
Basically API routes are for anything you fetch client-side or 3rd parties should have access to.
Asian black bear
If you have an external backend, sure. But Next.js is a fullstack framework, you don't necessarily need a separate backend.
English Lop
In our project we hide api keys for update data in nextjs api endpoints, coz default querys show this keys in network tab and this is not save
@English Lop In our project we hide api keys for update data in nextjs api endpoints, coz default querys show this keys in network tab and this is not save
Philippine Crocodile
Shouldn't API keys be stored in the backend and never even be sent to the browser? You don't need NextJS API endpoints to hide API keys.
Asian black bear
You can use them to proxy requests, for example to a different SaaS
Or to handle with CORS
Please don't hijack a thread of others for your own arguably unrelated questions. Regarding conventions in React and Next: move things around until it feels right. Next isn't opinionated like other frameworks such as Angular or Laravel. (Original message this reply was refering to, was deleted)
@Philippine Crocodile Shouldn't API keys be stored in the backend and never even be sent to the browser? You don't need NextJS API endpoints to hide API keys.
English Lop
Graphql tokens/jwt from backend visible at network tab, they refreshed with sessions, but we also hide them