首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >HapiJS:根据请求中传递的查询参数配置HapiJs?

HapiJS:根据请求中传递的查询参数配置HapiJs?
EN

Stack Overflow用户
提问于 2019-07-26 10:58:41
回答 2查看 153关注 0票数 0

我的API是用Hapijs编写的。并希望基于queryParamsrequest对象配置API。我有一个带有端点/test的API,queryParamstype=atype=b。如果type等于'a',那么需要在auth中传递false,如果type等于'b',则需要在auth中传递true

代码语言:javascript
复制
   {
        method: 'GET',
        path: '/media',
        handler: function (request, reply) {
            TestModule.getTestData(request.query).then(reply,(err) => {
                reply(err);
            });
        },
        config: {
            description: 'This is the test API',
            notes: 'Returns a message',
            tags: ['api', 'Test'],
            auth: false // Here I need to do something.
        },
    }

,你能告诉我我能做些什么吗?

我是这样做的:

代码语言:javascript
复制
   {
        method: 'GET',
        path: '/media',
        handler: function (request, reply) {
            TestModule.getTestData(request.query).then(reply,(err) => {
                reply(err);
            });
        },
        config: {
            description: 'This is the test API',
            notes: 'Returns a message',
            tags: ['api', 'Test'],
            auth: request.query.type==='a'?false:true // Here I need to do something.
        },
    }

但是得到一个错误的ReferenceError: request is not defined

EN

回答 2

Stack Overflow用户

发布于 2019-07-26 15:30:01

我不认为你能做到。

为什么不只是计算response.query.type,然后根据类型重定向呢?

代码语言:javascript
复制
 {
    method: 'GET',
    path: '/media',
    handler: function (request, reply) {

        const {
         type
        } = request.query;

        TestModule.getTestData(type).then(reply,(err) => {
            if(type === a) {
             // do something
            }
            //do something else
            reply(err);
        });
    },
    config: {
        description: 'This is the test API',
        notes: 'Returns a message',
        tags: ['api', 'Test'],
        auth: false // Here I need to do something.
    },
}
票数 0
EN

Stack Overflow用户

发布于 2019-07-26 19:06:29

您可能需要一个可选/尝试auth策略模式。详情请参见https://hapijs.com/api/#-routeoptionsauthmode。最终结果是

代码语言:javascript
复制
{
    method: 'GET',
    path: '/media',
    handler: function (request, reply) {
        if (request.query.type !== 'a' && !request.auth.isAuthenticated) {
            return reply(Boom.unauthorized('GET /media not allowed'));
        }

        TestModule.getTestData(request.query).then(reply,(err) => {
            reply(err);
        });
    },
    config: {
        description: 'This is the test API',
        notes: 'Returns a message',
        tags: ['api', 'Test'],
        auth: {
            strategy: 'your auth strategy',
            mode: 'try' // depending on your strategy. If you use a already existing strategy you probably want to use try as it will authenticate what it can and then you can still decide what to do in your handler.
        }
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57218669

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档