hapijs OnRequest 服务器扩展不访问服务器插件
hapijs OnRequest server extension doesn't access the server plugin
i18n 用于国际化目的。
当用户在 url 上发送语言代码时,我将像这样更改 i18n 的语言 http://localhost:8080/signup?language=en
为此,我正在使用服务器扩展 OnRequest。这样每条路线都会在全球范围内,
但问题是当我在 OnRequest 中访问 i18n 时它显示未定义的错误
但是在路由处理程序中它显示了 hapi-i18n 的所有属性
这是我的代码和输出
server.ext('onRequest', function(request, response){
console.log("pre handler");
console.log(request.i18n);
return response.continue();
});
输出:
未定义
但在处理程序中
{
method : 'POST',
path : "/signup",
config : {
tags : ['api'],
description : 'Customer signup',
},
handler: function(request, response){
console.log(request.i18n);
}
}
输出:
{ __: [Function],
__n: [Function],
getLocale: [Function],
setLocale: [Function],
getCatalog: [Function],
locale: 'en'
}
您需要使用 onPostAuth 扩展点,因为您正在使用的模块在请求对象生命周期中操作 onPreAuth 扩展点中的请求对象。
i18n 用于国际化目的。
当用户在 url 上发送语言代码时,我将像这样更改 i18n 的语言 http://localhost:8080/signup?language=en 为此,我正在使用服务器扩展 OnRequest。这样每条路线都会在全球范围内, 但问题是当我在 OnRequest 中访问 i18n 时它显示未定义的错误 但是在路由处理程序中它显示了 hapi-i18n 的所有属性 这是我的代码和输出
server.ext('onRequest', function(request, response){
console.log("pre handler");
console.log(request.i18n);
return response.continue();
});
输出: 未定义
但在处理程序中
{
method : 'POST',
path : "/signup",
config : {
tags : ['api'],
description : 'Customer signup',
},
handler: function(request, response){
console.log(request.i18n);
}
}
输出:
{ __: [Function],
__n: [Function],
getLocale: [Function],
setLocale: [Function],
getCatalog: [Function],
locale: 'en'
}
您需要使用 onPostAuth 扩展点,因为您正在使用的模块在请求对象生命周期中操作 onPreAuth 扩展点中的请求对象。