About Next API route
Answered
tachtme posted this in #help-forum
tachtmeOP
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.
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.
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.
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
tachtmeOP
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 backendtachtmeOP
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 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.
Philippine Crocodile
Why not make an API route for that 3rd party service in the backend?
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
@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