Netlify + Middy。不 return 响应客户端
Netlify + Middy. Does not return response to client side
我正在尝试使用 middy
为我的 netlify-lambda
函数创建一个中间件。中间件假定 return 响应我的 api 调用,如果它遇到错误语句并阻止执行该函数。并且响应无法return到客户端。
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 进行了测试,它打印了 'No token from browser' 但它只是停留在 'Sending request'。
对于在 Netlify 开发中做中间件的任何人,请避免使用 statusCode: 404
,因为它会导致您的服务器永远卡住。
此问题在 Netlify 中被标记为错误。参考:
Netlify dev server hangs if function returns 404
我正在尝试使用 middy
为我的 netlify-lambda
函数创建一个中间件。中间件假定 return 响应我的 api 调用,如果它遇到错误语句并阻止执行该函数。并且响应无法return到客户端。
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 进行了测试,它打印了 'No token from browser' 但它只是停留在 'Sending request'。
对于在 Netlify 开发中做中间件的任何人,请避免使用 statusCode: 404
,因为它会导致您的服务器永远卡住。
此问题在 Netlify 中被标记为错误。参考: Netlify dev server hangs if function returns 404