docs: added addServerHandler example to modules author guide (#22603)

This commit is contained in:
Sam K 2023-08-12 09:05:12 +02:00 committed by GitHub
parent 9fcd99eccc
commit 2a4867a23f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -375,6 +375,40 @@ export default defineNuxtModule({
})
```
#### Injecting Server Routes With `addServerHandler`
```ts
import { defineNuxtModule, addServerHandler, createResolver } from '@nuxt/kit'
export default defineNuxtModule({
setup(options, nuxt) {
const resolver = createResolver(import.meta.url)
addServerHandler({
route: '/api/hello',
handler: resolver.resolve('./runtime/server/api/hello/index.get.ts')
})
}
})
```
You can also add a dynamic server route:
```ts
import { defineNuxtModule, addServerHandler, createResolver } from '@nuxt/kit'
export default defineNuxtModule({
setup(options, nuxt) {
const resolver = createResolver(import.meta.url)
addServerHandler({
route: '/api/hello/:name',
handler: resolver.resolve('./runtime/server/api/hello/[name].get.ts')
})
}
})
```
#### Injecting Other Assets
If your module should provide other kinds of assets, they can also be injected. Here's a simple example module injecting a stylesheet through Nuxt's `css` array.