Anu*_*apu 6 routes node.js hapijs
我必须在路线中使用pre来调用方法.我正在使用hapi-request.我试图在路由声明中使用pre,但是我收到了一个错误.我错过了什么?
我原来的路线:
server.route({
method: 'POST',
path: '/searchUser',
config: User.searchUser
})
Run Code Online (Sandbox Code Playgroud)
使用Pre
server.route({
method: 'POST',
path: '/searchUser',
pre: validateUser,
config: User.searchUser
})
Run Code Online (Sandbox Code Playgroud)
错误
Error: Invalid route options (/searchUser) {
"method": "POST",
"path": "/searchUser",
"config": {}
}
?[31m
[1] "pre" is not allowed?[0m
Run Code Online (Sandbox Code Playgroud)
pre 应该在config对象中使用。
从Hapi中的先决条件文档中:
server.route({
method: 'GET',
path: '/',
config: {
pre: [
[
// m1 and m2 executed in parallel
{ method: pre1, assign: 'm1' },
{ method: pre2, assign: 'm2' }
],
{ method: pre3, assign: 'm3' },
],
handler: function (request, reply) {
return reply(request.pre.m3 + '\n');
}
}
});
Run Code Online (Sandbox Code Playgroud)
更新路线:
server.route({
method: 'POST',
path: '/searchUser',
config: {
handler: User.searchUser,
pre: [{ method: validate /* function to be called */ }]
}
);
Run Code Online (Sandbox Code Playgroud)