loveboat-postreqs
support hapi route post-requisites
(a transform written for loveboat)
Usage
This loveboat transform allows you to add route post-requisites in your hapi route configuration, whereas hapi on its own only supports pre-requisites.
Post-requisites run right after the handler, so they are especially useful for cleanup and for modifying the response.
In the example below, a post-requisite is used to upper-case the response.
// Ever wish this worked?server;
To use this transform,
- Make sure the loveboat hapi plugin is registered to your server.
- Tell loveboat that you'd like to use this transform by calling
server.routeTransforms([require('loveboat-postreqs')])
, possibly listing any other transforms you'd like to use.* - Register your routes using
server.loveboat()
rather thanserver.route()
.
* There are other ways to tell loveboat which transforms to use too! Just check-out the readme.
const Hapi = ;const Loveboat = ; const server = ;server; // 1. Register loveboatserver;
Limitations
The post-requisites are implemented as route-level onPostHandler
request extensions. In order to place them as near to the handler as possible, they are specified to run before the request extensions of all plugins that have already been registered and before all other route-level request extensions (unless the tight
option is false
). This leaves two blind spots– request onPostHandler
extensions that are created,
- in the same plugin that your route is registered and also before your route is registered.
- in a to-be-registered plugin and also opt to come
before
the extensions of the plugin in which your route is registered.
Basically, the post-requisites are run as near to the handler as possible using request extensions.
API
Options
The following options may be specified when the transform is registered,
tight
- whenfalse
, the post-requisites wont be forced to run beforeonPostHandler
extensions specified in other plugins. Defaults totrue
.
Route Definition
-
config.post
- an item or array of items of the format,- a function with signature
function(request, reply)
,request
- the request object.reply
- the reply interface, with identical usage as in a request extension. To continue normal execution of the request lifecycle,reply.continue()
must be called.
- an object with,
method
- a function with the signature described above.
Each item is a post-requisite. Post-requisites run after the route's handler, serially and in the order specified.
- a function with signature