Thomaschaaf
Thomaschaaf

Reputation: 18196

Why can't I create a HEAD route in Hapi?

According to the documentation https://hapijs.com/api/16.0.1#route-configuration a route method may not be of the type 'HEAD' but I do not understand why I can not override the behavior.

Upvotes: 2

Views: 403

Answers (2)

Gerardo Lima
Gerardo Lima

Reputation: 6703

As to further elaborate on @Ankh's response, you can check the request method property to abbreviate the response on the GET handler:

const getHandler = (req, h) => {
  // HTTP status 204 -> NO CONTENT
  if (req.method as string === 'head') return h.response().code(204)

  // continue the GET handler logic here
}

Upvotes: 0

Ankh
Ankh

Reputation: 5718

HEAD routes are automatically created with every GET route you define. Therefore there's very little reason you'd need to define your own.

If you want to optimize your handling of HEAD requests, simply check your GET handler for the method, and if it is head, return an empty response with the correct headers. This is only worth doing for some very expensive GET requests, where you are expecting clients to use HEAD directly.

The main reason not to support it, is that I am expecting very few developers to use this, but it will add an extra lookup for every HEAD request.

This has been already been addressed on Github.

Upvotes: 5

Related Questions