我正在尝试使用netlify-lambda为我的middy函数创建一个中间件。如果我的api调用遇到false语句并阻止执行该函数,则中间件假设返回对它的响应。并且响应不能返回到客户端。
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进行了测试,它打印了“没有来自浏览器的令牌”,但它只是停留在“发送请求”上。
发布于 2020-06-05 07:21:08
对于任何在Netlify dev中使用中间件的人来说,避免使用statusCode: 404,因为它会导致您的服务器永远卡住。
此问题在Netlify中被标记为bug。参考资料:如果函数返回404,则Netlify dev服务器挂起
https://stackoverflow.com/questions/62208349
复制相似问题