hapijs - How to access a request within a plugin? -


is there way access request (whether there's associated route or not) in hapi plugin?

i know have access server in plugin, seems if attempt set event handlers within context of plugin @ request, never fire.

for example, following works expected:

var hapi = require('hapi');     var server = new hapi.server();      server.connection({port: 3000});  server.start(function () {   console.log('hapi server started @ ', server.info.uri); });  server.ext('onrequest', function (request, reply) {   console.log(request);   return reply.continue(); });  server.route({                                             path: '/',                                              method: 'get',                  handler: function (request, reply) {         reply({yo: 'noid'});                       server.log(['trace'], request);       }                                     }); 

if try same thing within context of plugin, nothing happens:

// hapi app  var hapi = require('hapi');     var server = new hapi.server();      server.register({ register: require('sampleplugin') }, function (error) {   if(error) {     console.log('failed load plugin:', error);   } });  server.connection({port: 3000});  server.start(function () {   console.log('hapi server started @ ', server.info.uri); });  server.route({                                             path: '/',                                              method: 'get',                  handler: function (request, reply) {         reply({yo: 'noid'});                       server.log(['trace'], request);       }                                     });  // hapi plugin var sampleplugin = function (server, options, next) {                                                           'use strict';                                                                                            server.ext('onrequest', function (request, reply) {                                                       console.log(request);                                                                                   return reply.continue();                                                                              });                                                                                                      next();                                                                                               };                                                                                                       exports.register = logger;                                                                              exports.register.attributes = { pkg: require('./package.json') }; 

any appreciated. i'd happy hear alternative approaches. maybe i'm heading down wrong path (i'm new hapi). reading.

the issue code server.register() asynchronous, start server outside callback. need make sure server started when plugins registered. can fix code moving server.start() part callback of server.register():

var hapi = require('hapi'); var server = new hapi.server();  server.connection({port: 3000});  server.route({   path: '/',   method: 'get',   handler: function (request, reply) {     reply({yo: 'noid'});     server.log(['trace'], request);   } });  server.register({ register: require('./plugin') }, function (error) {   if(error) {     console.log('failed load plugin:', error);   }    server.start(function () {     console.log('hapi server started @ ', server.info.uri);   }); }); 

Comments

Popular posts from this blog

shopping cart - Page redirect not working PHP -

php - How to modify a menu to show sub-menus -

python - Installing PyDev in eclipse is failed -