Search code examples
node.jshapi.js

hapijs OnRequest server extension doesn't access the server plugin


i18n for internationalization purpose.

When user send the language code on url i will change the language of i18n like this http://localhost:8080/signup?language=en For that i am using the server extension OnRequest. So that on it will global for each route, but the problem occurs is when i access the i18n in OnRequest its shows undefined error But in the route handler it shows all the properties of the hapi-i18n Here is my code and output

server.ext('onRequest', function(request, response){
console.log("pre handler");
console.log(request.i18n);


return response.continue();
});

Output: Undefined

But in the handler

{
    method : 'POST',
    path   : "/signup",
    config : {
        tags        : ['api'],
        description : 'Customer signup',
    },
    handler: function(request, response){
        console.log(request.i18n);
    }
 }

Output:

{ __: [Function],
    __n: [Function],
   getLocale: [Function],
   setLocale: [Function],
   getCatalog: [Function],
   locale: 'en' 
}

Solution

  • You need to use the onPostAuth extension point as the module you are using manipulates request object in the onPreAuth extension point during request object lifecycle.