-
Hi, I need more guidance than the documentation provides. I'd like to put all related methods (GET, POST) into a single .server.ts file. What is correct sequence of folders & files for a route like: "/api/posts/1/comments" where 1 is the id for a post and we're expecting to GET back a json array of comments related to that post? Will the async GET function in the file will be provided the id of the post? TIA |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Hi @mrose! Your directory structure needs to look like this to handle comments on a post with an
In your export async function GET({
request: {
params: { id },
},
}: {
request: { params: { id: string } };
}) {
return new Response(`Post #${id}`);
} To get the request body, you need to use the web standard implementation to read request stream as a JSON: export async function POST({ request }: { request: Request }) {
const body = await request.json();
return new Response(`Comment data: ${JSON.stringify(body)}`);
} I will update the docs to explain the API route feature in a better way. |
Beta Was this translation helpful? Give feedback.
Hi @mrose!
Your directory structure needs to look like this to handle comments on a post with an
id
route parameter:In your
comments.server.ts
, you can access the params on therequest
:To get the request body, you need to use the web standard implementation to read request stream as a JSON:
I wi…