【发布时间】:2021-01-02 16:13:41
【问题描述】:
我正在尝试使用 Nylas 创建 webhook,在他们的 example 中,有一个中间件(代码如下)。我正在使用 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 }));
【问题讨论】:
-
您实际上是在向 Nylas 发出请求,还是只是在测试中间件?如果您提出请求,确实需要启用计费。
-
好吧,最终我会向 Nylas 发出请求,但目前在中间件中超时。这不应该发生,因为它只通过了
req.rawBody。我假设中间件只负责解析数据,但我没有其他猜测为什么它会超时,因为当我在本地 ngrok 上运行中间件时它工作得很好。只有当我在 firebase 函数下运行它时,它才会超时。我可以看到计费是如何成为一个问题的,但对于中间件来说不是那么多。你知道发生了什么吗?
标签: node.js typescript firebase express google-cloud-functions