Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/36.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 如何在hapi中验证标头_Node.js_Hapijs - Fatal编程技术网

Node.js 如何在hapi中验证标头

Node.js 如何在hapi中验证标头,node.js,hapijs,Node.js,Hapijs,我想添加一个中间件来检查标题值。如果标题中不存在预期的错误,那么我希望在API响应上抛出一个错误。下面的代码正在抛出错误,我可以在控制台中看到它,但我想将此错误发送给用户 const server = new Server({ host: config.get('host'), port: config.get('port' ) }); await server.register(require('@hapi/inert')); // eslin

我想添加一个中间件来检查标题值。如果标题中不存在预期的错误,那么我希望在API响应上抛出一个错误。下面的代码正在抛出错误,我可以在控制台中看到它,但我想将此错误发送给用户

const server = new Server({
        host: config.get('host'),
        port: config.get('port' )
    });

    await server.register(require('@hapi/inert')); // eslint-disable-line @typescript-eslint/no-var-requires

    server.register({
        plugin: AuthService
    })

    server.route(
        // mapping route paths with context path
        routes.map((datum) => {
            return {
                ...datum,
                path: `${config.get('basePath')}${datum.path}`
            };
        })
    );
插件

import { notFound } from '@hapi/boom';

export const AuthService = {
    name: 'authService',
    version: '1.0.0',
    register: async function (server: any, options: any) {
        throw notFound(`Unauthorized user not found`);
    }
};

嗯,我想你想得太快了。您应该查看Hapi的身份验证部分

如果标头不存在,您可以创建自定义策略并返回错误

此外,您还可以检查如何对所有路由使用
Joi.schema()