hapi.js - Extending the request with lifecycle events

Mike Frey
InstructorMike Frey

Share this video with your friends

Send Tweet
Published 6 years ago
Updated 3 years ago

Instead of using middlware, hapi provides a number of points during the lifecycle of a request that you can hook-in to provide additional functionality, called "extension events". This lesson will give you an introduction to using extension events, and show how they can be used to manipulate the request or response in-flight.

[00:00] Extension events are registered using the server.ext function. The first argument is the event name, and the second is the function to be executed for the event. The function takes "Request" and "Reply" arguments just like a route handler. To let the request continue though the lifecycle, I need to call reply.continue.

[00:19] If "Continue" is not called, the request will get stuck and never respond to the client. If I call "Reply" instead, HAPI would respond to the client too soon.

[00:28] To get an idea of when they are called during the request lifecycle, I'll register a function for each route extension event and log its name. I'm also logging when the route handler, itself, is called.

[00:43] After making a request, you can see that the events are displayed one on each line of my terminal. "On request" executes as soon as "Request" is received, before it has passed through the router.

[00:53] After the request has gone through the router, it needs to be authenticated. OnPreAuth runs before authentication, and onPostAuth runs after.

[01:04] Validation is handled next. OnPreHandler runs, then, the route itself. OnPostHandler runs after the handler. Then, the response payload is validated on pre-response runs, and the response is sent to the client.

[01:19] What are these extensions good for? Each extension can be used to manipulate the request or response at any point in the lifecycle. For example, in onRequest, I can force all requests to be interpreted as GETs to the route path.

[01:34] To do so, I'll use request.seturl, pass in the string/, and pass the string GET to request.setmethod. Now, when I make a post request to /foo, it still hits my defined route.