Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion docs/tutorial/getting-started/life-cycle/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ new Elysia()
.get('/2', () => 'Hello Elysia!')
```

Here we use `status(418)` which is the "I'm a teapot" status code. You can also use the string name directly: `status("I'm a teapot")`. See <DocLink href="/tutorial/getting-started/status-and-headers">Status and Headers</DocLink> for more on using status codes.

When `beforeHandle` returns a value, it will skip the handler and return the value instead.

This is useful for things like authentication, where you want to return a `401 Unauthorized` response if the user is not authenticated.
Expand Down Expand Up @@ -147,7 +149,7 @@ import { Elysia } from 'elysia'

new Elysia()
.onBeforeHandle(({ query: { name }, status }) => {
if(!name) return status(401)
if(!name) return status('Unauthorized')
})
.get('/auth', ({ query: { name = 'anon' } }) => {
return `Hello ${name}!`
Expand Down