用hapijs做一个基本的路由filter

我是新来的nodejs,我正在做一个API与hapijs来处理一些function从mi网站,我想能够使一个基地url像API / *,并使所有其他url的API /通过通过一些validation,但只做一次,这是我到目前为止:

server.route([ { method: ['GET', 'POST', 'PUT', 'DELETE', 'OPTIONS'], path: "/api/*", handler: function(request, reply){ is_authorized = auth(request.raw.req.headers['Authorization']); if(!is_authorized){ reply(response.generate_json(null, 'no autorizado', 'UNAUTHORIZED')).code(401); } } } ]); 

但它不工作,当我打电话任何其他url像API /套接字/无论它只是通过,即使它没有授权….有没有什么办法,我可以实现这个在hapijs?

你必须使用预处理器钩子

 server.ext('onPreHandler', (request, reply) => { if(request.path.startsWith("/api/")) { is_authorized = auth(request.raw.req.headers['Authorization']); if(!is_authorized) { reply(response.generate_json(null, 'no autorizado', 'UNAUTHORIZED')).code(401); } else { return reply.continue(); } } else { return reply.continue(); } }); 

如果你需要更多信息,请帮忙