API route , is it ideal ?
Unanswered
American Chinchilla posted this in #help-forum
American ChinchillaOP
I am making web app and nextjs (new to nextjs) as frontend and nodejs as backend. I want to make api request to nodejs server based on user interaction. Currently i make a /api and myendpoint/route.ts file to make request to nodejs server, and client component make a request to the api/myendpoint within the nextjs app. So the server URL will not be visible in the frontend (i have multiple endpoint request). My doubt - Is it the ideal way ? i do use serversideprops for certain cases.
15 Replies
English Spot
Using Next.js API routes as a proxy for your Node.js server provides several benefits. It hides your backend server's URL, enhancing security by preventing direct access to sensitive endpoints. This also allows you to centralize backend interactions in one place, making it easier to implement features like authentication, rate-limiting, or logging. Moreover, this approach adds flexibility, as you can switch or update your backend server’s URL without affecting the client-side code. It integrates seamlessly with Next.js features like getServerSideProps for server-side rendering, ensuring secure and efficient data fetching. Finally, by abstracting backend logic through Next.js, deployment is simplified since the frontend and backend can be unified under one server or independently hosted without changes to the frontend.
However, there are some downsides to this approach. It introduces an extra layer, creating a "double server hop" where requests first pass through the Next.js API routes before reaching the Node.js server, which can add latency. This complexity increases maintenance overhead, as debugging issues requires checking both the Next.js and Node.js logs. Additionally, if the majority of requests are proxied, it might lead to redundant functionality, where logic could have been directly implemented in either the Next.js API or the backend. In some cases, directly calling the backend from the client might be more efficient for non-sensitive operations. This layered approach should be carefully weighed against your project's performance and simplicity needs.
However, there are some downsides to this approach. It introduces an extra layer, creating a "double server hop" where requests first pass through the Next.js API routes before reaching the Node.js server, which can add latency. This complexity increases maintenance overhead, as debugging issues requires checking both the Next.js and Node.js logs. Additionally, if the majority of requests are proxied, it might lead to redundant functionality, where logic could have been directly implemented in either the Next.js API or the backend. In some cases, directly calling the backend from the client might be more efficient for non-sensitive operations. This layered approach should be carefully weighed against your project's performance and simplicity needs.
American ChinchillaOP
Cool .. so it is fine right?
@American Chinchilla yeah so you are using your api routes as proxy.
but don't make a fetch to your api route from your getServerSideProps()
American ChinchillaOP
yep .. btw my nodejs server is microservice and it has a proxy gateway..
@James4u but don't make a fetch to your api route from your getServerSideProps()
American ChinchillaOP
why?
it's unnecessary network trip from your next.js server to your server again
getServerSideProps() is a function that runs on your server
and you are making a request to your api route which is also your server
American ChinchillaOP
ok so should i make req from client ?
nope
what I meant is just make a request to your node.js server inside
getServerSideProps()American ChinchillaOP
cool
is your initial question solved? plz mark solution to close the thread
Original message was deleted
⬆️