【发布时间】:2020-06-05 15:21:08
【问题描述】:
我正在尝试使用middy 为我的netlify-lambda 函数创建一个中间件。中间件假设如果遇到错误语句并阻止执行该函数,则返回对我的 api 调用的响应。并且响应无法返回到客户端。
func.js
import withAuth from './middleware/auth';
import middy from '@middy/core';
const func = async (event, context, callback) => {
context.callbackWaitsForEmptyEventLoop = false;
try {
console.log('func');
// ... the function
} catch (error) {
console.log('locationCreate', error); // output to netlify function log
callback(null, statusCode: 500, body: JSON.stringify({error}));
}
};
exports.handler = middy(locationCreate).use(withAuth());
middleware.js
const withAuth = () => ({
before: (handler, next) => {
const { authorization } = handler.event.headers;
console.log('authorization', authorization);
if (!authorization) {
console.log('No token from browser');
return handler.callback(null, (statusCode: 400, body: JSON.stringify({message: 'Token not found'}));
}
return next();
},
});
module.exports = withAuth;
我使用 Postman 进行了测试,它打印了“浏览器中没有令牌”,但它只是停留在“正在发送请求”。
【问题讨论】:
标签: netlify