【发布时间】:2020-12-05 10:26:22
【问题描述】:
我有一个使用 loopback 3 并公开 REST API 的旧版应用程序,我想从客户端的传入请求中获取我的 JWT 令牌。我写了一个钩子来访问 req 对象以访问令牌。
Object.keys(app.dataSources).forEach((name: string) => {
if (camelCase(name) === name) {
app.dataSources[name].connector.observe('before execute', (ctx, next) => {
if (!ctx.req.uri) return next();
Object.keys(ctx.req).forEach(function(key) {
console.log(key, ctx.req[key]);
});
});
}
});
路线定义
accepts: [
{arg: 'codeValue', type: 'string', required: false, http: {source: 'query'}},
{arg: 'codeId', type: 'string', required: false, http: {source: 'query'}}
]
上述钩子执行时的输出
method GET
uri http://localhost:8010/api/v1/code/100
qs { codeValue: '' } <<== Issue 1
json true
form undefined
headers undefined <<== Issue 2
timeout undefined
以下是上面强调的问题:
- 即使我指定了查询参数 codeId,它也没有映射到 req 对象中
- req 中的标头为 undefined
卷曲命令
curl -H "codeId: TS01" -H "codeValue: 'TS 01" http://localhost:8081/api/v1/code/100
我们计划转为环回版本 4,但这是一个长期过程。任何指针都非常感谢。
【问题讨论】:
标签: javascript node.js rest loopbackjs loopback4