Next.js Discord

Discord Forum

Express-style routing for NextJS API routes?

Unanswered
Polar bear posted this in #help-forum
Open in Discord
Polar bearOP
I'm looking for a library that can help me break out of nextjs file-based routing in order to stand up a REST api.

I slapped react-admin into an existing (and very spaghettified) existing nextjs app. For my POC I set up a couple of simple route.ts api endpoints just to see it working.

Now I'm looking at expanding this out to the ~50 or so tables that we have. And when I imagine having to create:
foo/
route.ts
[id]
route.ts

with basically the same damn boilerplate code 50 times... i feel sad. much sad.
my thought was, there has gotta be a library that i can use where i just set up a single admin-api/[...rest]/route.ts and have it hand off responsibility for something that resembles express routing.

is there such a library my search has missed? is there a smarter way to do this?

7 Replies

Hunting wasp
i'm not sure i understand you right. do u want to handle all endpoints like
- /api/a
- /api/a/b/c
but not /api in only one route.ts?
What about you move the backend to a real, separate backend?

Next.js has a backend built it but it doesn’t not strictly mean that you have to use it for all use cases. Tho, you’d still have to type your return types and stuff manually.
If you don’t want to leave Next.js I would suggest you consider tRPC, they map your methods to endpoints exposed via /api/ inside Next.js. That’s basically what you’re looking for (I think) and it has a nice DX with their type safety and easy to use API
Polar bearOP
yeah @LuisLl i know the right answer is to have a separate backend, but im constrainted by existing team culture. like, when i just offhand mentioned i was going to set up react-admin in a separate nextjs app, there was major pushback from one teammember.

ahhh trpc? i will look at that, ive heard of it but not been exposed to it. thanks!
Polar bearOP
hmmm react-admin has a trpc data provider