hapi.js - Extending the request with lifecycle events

Mike Frey
InstructorMike Frey
Share this video with your friends

Social Share Links

Send Tweet
Published 8 years ago
Updated 5 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.

egghead
egghead
~ 2 hours ago

Member comments are a way for members to communicate, interact, and ask questions about a lesson.

The instructor or someone from the community might respond to your question Here are a few basic guidelines to commenting on egghead.io

Be on-Topic

Comments are for discussing a lesson. If you're having a general issue with the website functionality, please contact us at support@egghead.io.

Avoid meta-discussion

  • This was great!
  • This was horrible!
  • I didn't like this because it didn't match my skill level.
  • +1 It will likely be deleted as spam.

Code Problems?

Should be accompanied by code! Codesandbox or Stackblitz provide a way to share code and discuss it in context

Details and Context

Vague question? Vague answer. Any details and context you can provide will lure more interesting answers!

Markdown supported.
Become a member to join the discussionEnroll Today