Disable API routing for subfolder
Answered
Japanese jack mackerel posted this in #help-forum
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 route11 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 routeAnswer
Western paper wasp
See “Dynamic API Routes†here: https://nextjs.org/docs/pages/building-your-application/routing/api-routes#dynamic-api-routes
@Japanese jack mackerel 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?
Rewrites are good, no problems with it. Have you tried?
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
•
•
in your project as separate files — since they have a different parent folder they don’t conflict
•
/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 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
Japanese jack mackerelOP
i am used to simplify my posts - there are no cats, only rooms 😉 Thanks!
Western paper wasp
got it haha
/api/sockets/[room_id].js should work great thenIt’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 accordinglyIf that works for you, feel free to accept my answer