mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-22 13:45:18 +00:00
docs: add examples for index.ts files on server routes (#23390)
Co-authored-by: Sébastien Chopin <seb@nuxtlabs.com> Co-authored-by: Sébastien Chopin <seb@nuxt.com>
This commit is contained in:
parent
7005a66d9a
commit
d788577fc2
@ -180,6 +180,38 @@ Given the example above, fetching `/test` with:
|
||||
- **POST** method: Returns `Test post handler`
|
||||
- Any other method: Returns 405 error
|
||||
|
||||
You can also use `index.[method].ts` inside a directory for structuring your code differently.
|
||||
|
||||
**Example:**
|
||||
|
||||
```ts [server/api/foo/index.ts]
|
||||
export default defineEventHandler((event) => => {
|
||||
// handle the `api/foo` endpoint
|
||||
})
|
||||
```
|
||||
|
||||
This is useful to create API namespaces.
|
||||
|
||||
**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`.
|
||||
|
Loading…
Reference in New Issue
Block a user