Skip to content

Commit

Permalink
docs(routing): readd missing Object Syntax Event Handler
Browse files Browse the repository at this point in the history
  • Loading branch information
samydoesit committed Mar 19, 2024
1 parent d2881a8 commit 3008b62
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions docs/1.guide/2.routing.md
Expand Up @@ -266,3 +266,25 @@ export default defineNuxtConfig({
})
```
::

## Object Syntax Event Handler

H3 introduces support for defining event handlers using an Object syntax. With this approach, you can define hooks that run before or after each handler, such as authentication or compression middleware.

```ts
const auth = defineRequestMiddleware((event) => {
event.context.auth = { name: 'admin' }
})

const compression = defineResponseMiddleware((event) => {
// Example: https://stackblitz.com/edit/github-mb6bz3
})

export default eventHandler({
onRequest: [auth],
onBeforeResponse: [compression],
async handler(event) {
return `Hello ${event.context.auth?.name || 'Guest'}`
},
})
```

0 comments on commit 3008b62

Please sign in to comment.