If I work with backend developer(kotlin, spring), Is it meaningless using Nextjs api?

1.2k views Asked by At

My project using next13, kotlin spring. I want to upload image.(I only use nextjs, there is other backend developer who use kotlin spring)

Way I think.

A. Just request rest api to spring server directly from the client (‘use client’ component) ex)When put the image to input type file, just do axios post to spring server api.(like normal way we did when we use react)

B. Request api to next server(app/api/blahblah) and then request again to spring server.

If A is good way.. what is next server for? Is Using nextjs only good for full stack developer..who work alone? (To be honest, I don't quite understand what role next server plays when working in a company where the backend and frontend collaborate)

If B is good way.. Could you please explain how it works? I guess axios/fetch post to next server and get the req data, and then axios/fetch post again from the next server to backend server(spring server in my case). Am I right??

2

There are 2 answers

0
Jonathan Ebiyomare On

Sending the request from the frontend directly to the backend would be the right choice. Although nextjs can be used to create full-stack applications, it can also be used to develop front-end applications.

0
Rishi Ranjan On

Both approaches will work, and the choice literally depends on your specific project requirements and architecture, for example,

Way A - the direct request one, the client(Next Js) directly sends a request to the spring server to upload the image, this is a straightforward and and efficient way to handle image uploads, why efficient because with this way you will have lower latency, as there is no additional round trip request between the next js server and the spring server, which can apparently reduce latency, but one small cons in this way, is that you will not avail next js SSR benefits to its fullest then, as image uploads don't require SSR, so this may not fully utilize next js capabilities.

Way B - in this approach, the client sends a request to a next.js server route, which then forwards the request to the Spring server for image upload, so here you will have centralized control and SSR flexibility i.e. if your application requires SSR for some routes , this approach allows you to use Next.js for SSR on those routes, and on cons side there is an increased complexity involved which makes your application architecture complex, and obviously you will experience a higher latency in response.

In summary, if you're using Next.js primarily for SSR, approach A is simpler and more efficient for handling image uploads. However, if you need centralized control or want to take advantage of Next.js's capabilities for certain routes, approach B can be a good choice.

In approach B, you're correct in your understanding. The client (using Axios or Fetch) would send a POST request to a Next.js API route (/api/blahblah), and in this API route, you can make another Axios or Fetch request to the Spring server to handle the image upload. The Next.js server acts as an intermediary in this process, allowing you to implement additional logic or handle SSR if needed.