node.js - HapiJS:根据request中传入的查询参数配置HapiJs API?
问题描述
我的 API 是用 Hapijs 编写的。并希望基于对象配置 API queryParams
。request
我有一个带有端点的 API,/test
并且queryParams
是type=a
或type=b
。如果type
等于'a'
,那么我需要传入false
,auth
如果type
等于,'b'
那么我需要传入true
.auth
{
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.
},
}
你能告诉我我能做什么吗?
我正在这样做:
{
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
。
解决方案
我不认为你能做到这一点。
为什么不只评估 response.query.type 然后根据类型重定向?
{
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.
},
}
推荐阅读
- amazon-web-services - 为超集终止 NLB 上的 HTTPS
- reactjs - 如何访问另一个 jsx 文件中的状态/道具
- java - 子包中的类名相同
- git - 你如何向 git 添加一个包含子模块的文件夹,这些子模块是 npx(创建反应应用程序)所做的基本项目
- ssl - 无法通过 CLI 建立 tls1.2 连接
- java - 在不使用 java 集合库的情况下将 int 添加到数组中的基本代码?
- python - 在抽搐聊天中选择聊天框?硒
- arrays - 按 int 值 Bash 排序字符串数组的问题
- python - 如何使用 Python 更改终端的颜色
- css - 可排序的 xsl,用于运行 HTML