mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-22 13:45:18 +00:00
docs: include example for server utilities (#21337)
This commit is contained in:
parent
6bb0a849fe
commit
2fccfa2d5c
@ -102,6 +102,27 @@ Server routes are powered by [unjs/h3](https://github.com/unjs/h3) which comes w
|
|||||||
|
|
||||||
You can add more helpers yourself inside the `~/server/utils` directory.
|
You can add more helpers yourself inside the `~/server/utils` directory.
|
||||||
|
|
||||||
|
For example, you can define a custom handler utility that wraps the original handler and performs additional operations before returning the final response.
|
||||||
|
|
||||||
|
**Example:**
|
||||||
|
|
||||||
|
```ts [server/utils/handler.ts]
|
||||||
|
import type { EventHandler } from 'h3'
|
||||||
|
|
||||||
|
export const defineWrappedResponseHandler = (handler: EventHandler) =>
|
||||||
|
defineEventHandler(async (event) => {
|
||||||
|
try {
|
||||||
|
// do something before the route handler
|
||||||
|
const response = await handler(event)
|
||||||
|
// do something after the route handler
|
||||||
|
return { response }
|
||||||
|
} catch (err) {
|
||||||
|
// Error handling
|
||||||
|
return { err }
|
||||||
|
}
|
||||||
|
})
|
||||||
|
```
|
||||||
|
|
||||||
## Server Types
|
## Server Types
|
||||||
|
|
||||||
::alert{type="info"}
|
::alert{type="info"}
|
||||||
|
Loading…
Reference in New Issue
Block a user