Next.js Discord

Discord Forum

How to use custom routes without /api path "nextjs"

Answered
Masai Lion posted this in #help-forum
Open in Discord
Original message was deleted.
Answered by Plague
Yeah it's likely Server Actions will change the way we think about/perform these types of interactions.
View full answer

10 Replies

You wanna call your API within your next js app or within a server??
Original message was deleted
Are you sending this POST request from the client?
Original message was deleted
Yeah, in this case you're going to need to make an API route to handle this, the request to your back-end needs to come from a server not a client, clients are not trust-worthy hence the CORS error, I had a similar issue when I was trying to POST data from the client to a google form, the request needed to come from a server in order for my error to subside.
Are you talking about the compute time of the functions on the server? Are you worried about the functions consuming more of that?
Yeah it's possible that it's do the nature of how Serverless functions work, they start cold so it takes them a while to get up and running, but, once they are warm the time decreases significantly, IMO, it's better for this to be done on the server than eating the client bandwidth, people with slower internet connections will suffer greatly.

I'd suggest reviewing the limits of the hosting service you're using and gaging the execution units/time limits and if you're receiving so much traffic or consuming so much bandwidth that you're worried you'll hit the limit, I don't know if anything can be done besides upgrading, this type of action should be performed on the server for the most optimal performance afaik. Hope this helped.
Yeah it's likely Server Actions will change the way we think about/perform these types of interactions.
Answer
Original message was deleted
you could also simply use a reverse proxy by using [rewrites](https://nextjs.org/docs/app/api-reference/next-config-js/rewrites)
that's the solution to CORS-related issues if you don't want to update CORS or you don't want to let the world know your backend domain
Original message was deleted
Giant panda
if you want to call it from the browser to ur backend api, what's the issue with a normal fetch()? and yea allow the domains from which u want to hit in ur BE.