-1

I have seen many programmers write their APIs with PHP or ASP.NET or ... and request those APIs from Next.js. We know that Next.js can implement API. Is there a reason why most programmers do not write their APIs with Next.js? Maybe the reason is better performance or Next.js API is not suitable for large projects or ...?

And in some programs, we write our API with Next.js and from components request to them (getServerSideProps), and from those API, for example, we connect to the database and get data. My question is why should we write API at all and why not connect directly to the database from the getServerSideProps function and receive the required data? which one is better?

2
  • Next.js is a front-end framework and the sever functionality there is focused mainly on SSR. Consider using ExpressJS for your back-end (and if I'm not mistaken Next.js uses Express as a server)
    – JSEvgeny
    Jan 8, 2022 at 17:30
  • If you are used to automatic server reload with nodemon then this wouldn't work out of the box with Next.js and would need some configuration. However, choosing to opt out of Next.js' API and replace it with another server would make you lose out on a lot that Vercel has to offer
    – Y H R
    Jan 8, 2022 at 19:32

1 Answer 1

0

Actually use case are different Next.js is similar to react but you can have SSR with it for your api you can use next.js api route i mean you can connect to your database and fetch data through next.js api but if you want to use next.js as backend or api it is not the use case you can try node for api. and also getServerSideProps is a function to make a page SSR not for api or something else.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.