Need help in structuring api routes
Unanswered
Devon Rex posted this in #help-forum
Devon RexOP
Hi guys, I wordering whether this is the right way to structure my api routes or not. Because, I think, by doing this way, I need to create 10s of folders and route.ts for each end point. Is there a better way or a shortcut? Your help much appreciated.
21 Replies
without using dynamic routes, this is the only way .... i would say that if they are different enough its fine... also, you could do some of these with query params or delete req type
@Devon Rex Hi guys, I wordering whether this is the right way to structure my api routes or not. Because, I think, by doing this way, I need to create 10s of folders and route.ts for each end point. Is there a better way or a shortcut? Your help much appreciated.
yes it looks fine to me. each route in nextjs requires a folder, so having tons of folders is to be expected
that said, i would use server actions over route handlers
@riský without using dynamic routes, this is the only way .... i would say that if they are different enough its fine... also, you could do some of these with query params or delete req type
Devon RexOP
Thank you. I suppose I have stick with this structure until they come up with some solution. In Express.js, it is very easy to create routes; just methods in a route file.
@joulev that said, i would use server actions over route handlers
Devon RexOP
I am waiting until server actions become stable. Let's see
@Devon Rex I am waiting until server actions become stable. Let's see
they are stable now 🎉 *
* in canary (so shouldn't have to wait long until stable release includes it)
* in canary (so shouldn't have to wait long until stable release includes it)
@joulev yes it looks fine to me. each route in nextjs requires a folder, so having tons of folders is to be expected
Hilsa shad
can we use the server components in production based environment
Hilsa shad
I am as of now that is not stable so would that be a trouble anymore ?
@Hilsa shad I am as of now that is not stable so would that be a trouble anymore ?
server components in nextjs are stable since nextjs 13.4
Hilsa shad
@joulev thanks for the information
@joulev server components in nextjs are stable since nextjs 13.4
Yellow croaker
So using server actions on production level application is totally safe now? About month ago understood that they are not ready for production level.
And dont i still Have to tag server action to Be experimental?
And dont i still Have to tag server action to Be experimental?
Officially speaking, server actions is not yet stable
But it will be stable when nextjs 14 is released in less than a week
So I’d say yes, it’s safe now
@joulev Server components != server actions
Yellow croaker
Oh right My Bad! Well If nextjs 14 is released in under week, then i might start working with server actions thanks
@Yellow croaker Oh right My Bad! Well If nextjs 14 is released in under week, then i might start working with server actions thanks
Yes. They already removed the experimental tag in the upstream nextjs repo: https://github.com/vercel/next.js/pull/57145
Komondor
your structure can be a bit more compact if you make it more RESTful, such as
POST /api/friendrequests/ instead of send-friend-request
DELETE /api/friendrequests/id instead of cancel-request
POST /api/friendrequests/ instead of send-friend-request
DELETE /api/friendrequests/id instead of cancel-request
@riský without using dynamic routes, this is the only way .... i would say that if they are different enough its fine... also, you could do some of these with query params or delete req type
yeah that is what i was trying to say here (but with too fancy terms i think)
@riský yeah that is what i was trying to say here (but with too fancy terms i think)
Komondor
ah I see that now. I'm getting used to the terms as well