首页 > 解决方案 > req.on('end') 没有运行 Firebase express Node.js Nylas

问题描述

我正在尝试使用 Nylas 创建 webhook,在他们的示例中,有一个中间件(代码如下)。我正在使用 Firebase 端点,我的代码是用打字稿编写的。我尝试使用 ngrok 在新项目中本地复制 Nylas 示例,中间件成功返回 200 状态。但是,当我在 Firebase 函数中使用完全相同的代码时,中间件会停止req.on('end超时,所以我收到console.log('2')但不是console.log('3')。Firebase 声称,在设置计费之前,第三方服务受到限制。这会影响中间件吗?为什么req.on('end不开火?

  req.rawBody = "";
  console.log("1");
  req.on("data", (chunk) => (req.rawBody += chunk));
  req.on("error", (err) => res.status(500).send("Error parsing body"));
  console.log("2");
  req.on("end", () => {
    console.log("3");
    // because the stream has been consumed, other parsers like bodyParser.json
    // cannot stream the request data and will time out so we must explicitly parse the body
    try {
      console.log("4");
      req.body = JSON.parse(req.rawBody);
      console.log(req.body);
      next();
    } catch (err) {
      res.status(500).send("Error parsing body");
    }
  });
});
app.use(bodyParser.urlencoded({ limit: "5mb", extended: true }));

标签: node.jstypescriptfirebaseexpressgoogle-cloud-functions

解决方案


解决方案:

通过使用这种方法解析数据得到状态 200 。


推荐阅读