Reputation: 89
I have an api that dynamically pulls a post. localhost/api/post/[postId]
I want to extend functionality to retrieve the comments of the post at the URL localhost/api/post/[postId]/comments
What is the correct way to structure this in Next.js? I am thinking to create a dynamic route and check if the query equals comment? localhost/api/post/[postId]/[comment]
Upvotes: 0
Views: 278
Reputation: 26
The following file structure should work:
/pages/api/post/[postId]/index.js //Route: /api/post/1
/pages/api/post/[postId]/comments.js //Route: /api/post/1/comments
Besides that, just to follow good practices, I would recommend that you rename "post" to "posts", like that:
/pages/api/posts/[postId]/comments.js
Upvotes: 1