docs: update and shorten server example (#22583)

This commit is contained in:
@beer 2023-08-11 14:43:05 +06:00 committed by GitHub
parent 99375d688c
commit 20efac7d57
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -152,7 +152,10 @@ Server routes can use dynamic parameters within brackets in the file name like `
**Example:**
```ts [server/api/hello/[name\\].ts]
export default defineEventHandler((event) => `Hello, ${event.context.params.name}!`)
export default defineEventHandler((event) => {
const name = getRouterParam(event, 'name')
return `Hello, ${name}!`
})
```
You can now universally call this API using `await $fetch('/api/hello/nuxt')` and get `Hello, nuxt!`.