Next.js Discord

Discord Forum

Disable API routing for subfolder

Answered
Japanese jack mackerel posted this in #help-forum
Open in Discord
Japanese jack mackerelOP
Hi, I need to disable API routing for a subfolder, so that everything that calls e.g. /api/cats/big_cat is only calling /api/cats module without looking for a big_cat module (big_cat is only used as a parameter inside the code, like an ID). I have looked at rewrites but my feeling is that the folder is still appended and interpreted. Any ideas?
Answered by Western paper wasp
You need to create your route as

/api/cats/[id].js, and then you will be able to access id as a parameter in your API route
View full answer

11 Replies

Western paper wasp
You need to create your route as

/api/cats/[id].js, and then you will be able to access id as a parameter in your API route
Answer
Western paper wasp
Japanese jack mackerelOP
thanks @joulev @Western paper wasp , so my router would be this [id].js file? I will try that. My case is a bit special though, as this [id].js file is not an ordinary REST route but a websocket endpoint for rooms. In reality its /api/sockets/[room_id].js So the id is the room id for the websocket server (using next-ws).
Western paper wasp
I'm confused — if the API route is /api/sockets/[room_id].js then where is the /api/cats route?
If those are separate routes, you can have both
• /api/sockets/[room_id].js
• /api/cats/[cat_id].js
in your project as separate files — since they have a different parent folder they don’t conflict
Western paper wasp
got it haha
/api/sockets/[room_id].js should work great then
It’s just like a normal API route except the “sub-routes” will be received by the parent, which can read the room_id parameter and act accordingly
If that works for you, feel free to accept my answer