【问题标题】:Netlify + Middy. Does not return response to client sideNetlify + Middy。不向客户端返回响应
【发布时间】: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


    【解决方案1】:

    对于在 Netlify 开发中做中间件的任何人,请避免使用statusCode: 404,因为它会导致您的服务器永远卡住。

    这个问题在 Netlify 中被标记为 bug。参考: Netlify dev server hangs if function returns 404

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-10-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-08-11
      • 2014-11-13
      • 2021-03-12
      相关资源
      最近更新 更多