我正在使用middy为我的 AWS lambda 代码处理中间件。下面是我的代码:
const middy = require('middy')
const middlewares = require('middy/middlewares')
const handlerWrapper = (handler) => middy((event, context, callback) => {
context.callbackWaitsForEmptyEventLoop = false;
let code = 200
context.send = (body) => callback(null,{
statusCode: code,
body: JSON.stringify(body)
})
context.status = (_code) => {
code = _code
return context
}
return handler(event, context ,callback)
}).use(middlewares.jsonBodyParser()).use(middlewares.cors())
问题是,我的前端正在发送一个名为的自定义标头authorizationv2
,当我的前端尝试向我的 AWS lambda 代码发送请求时,浏览器将显示此错误
Access to XMLHttpRequest at 'https://apiv2.website.com/data/list/' from origin 'http://admin.website.com' has been blocked by CORS policy: Request header field authorizationv2 is not allowed by Access-Control-Allow-Headers in preflight response.
所以我假设要解决这个问题,我需要middy
允许自定义标头authorizationv2
。我不完全确定如何使用 middy。