From 1c563cd122ce38100fe5ba89b5fa6474c05331d0 Mon Sep 17 00:00:00 2001 From: Filip Weidemann Date: Sun, 24 Sep 2023 22:06:22 +0200 Subject: [PATCH 1/3] docs: add examples for index.ts files on server routes --- .../2.guide/2.directory-structure/1.server.md | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/docs/2.guide/2.directory-structure/1.server.md b/docs/2.guide/2.directory-structure/1.server.md index 52545df79de1..5d17696a451b 100644 --- a/docs/2.guide/2.directory-structure/1.server.md +++ b/docs/2.guide/2.directory-structure/1.server.md @@ -180,6 +180,38 @@ Given the example above, fetching `/test` with: - **POST** method: Returns `Test post handler` - Any other method: Returns 405 error +You may also name files like `index.[verb].ts` if you want to compose your routes inside different folders for better readability. + +**Example:** + +```ts [server/api/foo/index.ts] +export default defineEventHandler((event) => => { + // handle the `api/foo` endpoint +}) +``` + +This is especially powerful if you want to enable different logic based on your HTTP verbs on the root of the API namespace as well as other logic that should get own routes inside the `foo` namespace: + +**Example:** + +```ts [server/api/foo/index.get.ts] +export default defineEventHandler((event) => => { + // handle GET requests for the `api/foo` endpoint +}) +``` + +```ts [server/api/foo/index.post.ts] +export default defineEventHandler((event) => => { + // handle POST requests for the `api/foo` endpoint +}) +``` + +```ts [server/api/foo/bar.get.ts] +export default defineEventHandler((event) => => { + // handle GET requests for the `api/foo/bar` endpoint +}) +``` + ### Catch-all Route Catch-all routes are helpful for fallback route handling. For example, creating a file named `~/server/api/foo/[...].ts` will register a catch-all route for all requests that do not match any route handler, such as `/api/foo/bar/baz`. From bf3fd3a860306964eb449ef55a08169836bed11a Mon Sep 17 00:00:00 2001 From: Filip Weidemann Date: Tue, 26 Sep 2023 19:20:22 +0200 Subject: [PATCH 2/3] docs: Update docs/2.guide/2.directory-structure/1.server.md MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Sébastien Chopin --- docs/2.guide/2.directory-structure/1.server.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/2.guide/2.directory-structure/1.server.md b/docs/2.guide/2.directory-structure/1.server.md index 5d17696a451b..e9a5ea1e76a1 100644 --- a/docs/2.guide/2.directory-structure/1.server.md +++ b/docs/2.guide/2.directory-structure/1.server.md @@ -180,7 +180,7 @@ Given the example above, fetching `/test` with: - **POST** method: Returns `Test post handler` - Any other method: Returns 405 error -You may also name files like `index.[verb].ts` if you want to compose your routes inside different folders for better readability. +You can also use `index.[method].ts` inside a directory for structuring your code differently. **Example:** From 6c2d1e00d7d279fa9b7398adde49b5180277d2e9 Mon Sep 17 00:00:00 2001 From: Filip Weidemann Date: Tue, 26 Sep 2023 19:20:39 +0200 Subject: [PATCH 3/3] docs: Update docs/2.guide/2.directory-structure/1.server.md MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Sébastien Chopin --- docs/2.guide/2.directory-structure/1.server.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/2.guide/2.directory-structure/1.server.md b/docs/2.guide/2.directory-structure/1.server.md index e9a5ea1e76a1..dd394d4a057a 100644 --- a/docs/2.guide/2.directory-structure/1.server.md +++ b/docs/2.guide/2.directory-structure/1.server.md @@ -190,7 +190,7 @@ export default defineEventHandler((event) => => { }) ``` -This is especially powerful if you want to enable different logic based on your HTTP verbs on the root of the API namespace as well as other logic that should get own routes inside the `foo` namespace: +This is useful to create API namespaces. **Example:**