Member-only story

Understanding the HttpContext — Let’s Learn Adonis 5, Lesson 1.3

Tom Gobich | Adocasts.com
5 min readNov 18, 2021

--

View all lessons in the Let’s Learn Adonis 5 series here.

The HttpContext object is an object that we’ll be heavily working with as we develop our Adonis application. It’s an object that’s going to be provided to us via our middleware, route handlers, HTTP hooks, and exception handlers as we progress throughout our request’s lifecycle.

It’s an object that’s unique to each of our application’s individual requests. So, anytime a user requests a page, submits a form, submits an API request, etc we’ll receive a unique HttpContext object about that user’s request.

HttpContext Mutation Behavior

The way Adonis is going to pass our HttpContext object from one request lifecycle method to the other is by reference. This means that if we mutate our HttpContext object inside our middleware that mutation will remain intact when we receive that request within our route handler.

If an error were then thrown for that request in our route handler, the same mutation we made within our middleware will again be carried through to the exception handler.

By mutating our HttpContext, we can easily and effectively pass additional information, for or about a request, as that request progresses from one portion of our application to another.

HttpContext Contents

What our application’s HttpContext contains actually varies depending on which packages our application has installed and registered.

For example, in this series, we’ve yet to cover or install the Auth module. Since we’ve yet to install the Auth module, we’re not going to have an auth property on our HttpContext. Once we do install and configure the Auth module within our application, however, we'll then have an auth module on our HttpContext that'll allow us to authenticate a user, deauthenticate a user, get the currently authenticated user, and more.

Our Application’s HttpContext

Let’s start by understanding what our web structured Adonis application has within its HttpContext

--

--

Tom Gobich | Adocasts.com
Tom Gobich | Adocasts.com

Written by Tom Gobich | Adocasts.com

Learn AdonisJS, NodeJS, JavaScript and more through in-depth lessons, screencasts, and livestreams at adocasts.com

No responses yet