Skip to content

Commit

Permalink
feat: add typehinting example to context for controllers
Browse files Browse the repository at this point in the history
  • Loading branch information
thetutlage committed May 18, 2021
1 parent fd9c595 commit f8ca11c
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion content/guides/http/context.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ summary: HTTP request context holds all the relevant information related to a gi

HTTP context is a request-specific object that holds the information like the `request body`, `cookies`, `headers`, the currently `logged in user`, and much more for a given HTTP request.

The HTTP context is passed by reference to the route handler, middleware, HTTP hooks, and exception handler.
The HTTP context is passed by reference to the route handler, middleware, HTTP hooks, and the exception handler.

```ts
Route.get('/', ({ request, auth, response }) => {
Expand All @@ -31,6 +31,18 @@ Route.get('/', ({ request, auth, response }) => {
})
```

Make sure to define the HTTP context type explicitly when accessing the context inside a controller method.

```ts
import { HttpContextContract } from '@ioc:Adonis/Core/HttpContext'

class HomeController {
public async index({ request, response }: HttpContextContract) {

}
}
```

## Differences from the express `req` and `res` objects?

You will not see any `req` or `res` objects in AdonisJS. Everything, including the request and the response is part of the HTTP context.
Expand Down

0 comments on commit f8ca11c

Please sign in to comment.