hapijs - How to call Hapi Plugins for certain routes? -
im using hapi 12.1.
trying figure out how call extension points on routes.
for example : '/hello' want call 3 different extension points work on 'onrequest' step.
for: '/goodbye' want call different extension point works on 'onrequest' different operation , 'onpreauth' step.
for: '/health' dont call extension points, , drop handler straight away..
i have tried various ways create plugin, define routes, , extension points. seems the extension points global, , dont operation on plugin's scoped routes.
what missing?
you have access path on extension points, using request.route.path
. that, can define want run, depending on path. example:
server.ext('onpreauth', function (request, reply) { switch(request.route.path) { case '/test1': case '/test2': // break; case '/test3': // else break; } reply.continue(); });
alternatively, can make dependent on route configuration:
server.ext('onpreauth', function (request, reply) { if(request.route.settings.plugins.runpreauth) { // } reply.continue(); });
then, define in route configurations:
server.route({ method: 'get', path: '/test1', handler: function(request, reply) { reply({result: 'ok'}); }, config: { plugins: { runpreauth: true } } });
Comments
Post a Comment