API
Declare the HTTP method and URL as the file name.
API routing
- server/api/hello.ts → GET /api/hello
- server/api/notes.get.ts → GET /api/notes
- server/api/notes.post.ts → POST /api/notes
- server/api/users/[id].patch.ts → PATCH /api/users/:id
- server/api/files/[...path].get.ts → GET /api/files/*
- *.ts files that omit the method suffix are processed as GET.
- If you have a lot of files, organize them into folders as deep as you need, such as server/api/admin/users/*.ts.
- [[page]] is an optional path, and HEAD and OPTIONS files are also supported.
- If the default export function returns a string, it will be an HTML response, and if it returns an object, it will be a JSON response.
- Unused parameters are prefixed with _ like _data and are not consumed as void data.
default handler
The context contains app, request, params, and requestId for request tracking. Come in. If you return the response directly, status, You can use the header and streaming body as is.
export default async (_input: unknown, context: ApiContext) => ({
id: context.params.id,
requestId: context.requestId,
count: await db.note.count()
});