S
S
semki0962020-08-15 02:53:40
React
semki096, 2020-08-15 02:53:40

What are the use cases for api-routes in Next.js?

There is such a thing https://nextjs.org/docs/api-routes/introduction As far as I understand, this api can be used effectively, because that's what it was made for. And as far as I understand, it can be used inside the application, that is, not necessarily as a source for third-party applications. But I can’t understand how to use it, and why if we can make a simple query to the database? Maybe I'm wrong, but it seems to be used together with SWR, maybe someone knows, I would be grateful for an explanation.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
H
hzzzzl, 2020-08-15
@semki096

as I understand it, this was done in order not to raise any other express on the server in parallel with next.js (especially if this express is needed only to respond to api)
https://www.youtube.com /watch?v=61TngxLrP_0&t=1684s
here uncle talks more about it with pictures, see how one step "accessing api" is skipped, because it is now the responsibility of NEXT himself,
5f37a32019ef6518458150.png
but when I last looked at the documentation, there was no way * store an instance of an open connection to the database, that is, you have to somehow stupidly open a connection every time you access api-route, which discouraged me from going deeper - it’s easier and better to raise express for this, and distribute the app from it to next, still
* UPD seems to still have options
https://stackoverflow.com/questions/61492595/next-...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question