在我可以在 Express 中处理请求主体之前,Firebase 函数会解析请求主体

Firebase functions parsing request body before I can handle it in Express

我正在尝试处理 Firebase 函数中的无效请求,因此使用无效 JSON 发出 post 请求,目的是快速处理它。但是我在它到达快速层之前得到 400 错误 'SyntaxError: Unexpected token a in JSON at position 20',最糟糕的是函数运行 60 秒直到它遇到超时错误。

我的函数

import * as functions from 'firebase-functions';
import * as express from 'express';
import * as admin from 'firebase-admin';

admin.initializeApp();


const app = express();

app.use((err: any, req: any, res: any, next: any) => {
  res.json({ error: 'invalid request' });
  next(err);
});

app.post('/test', (req: any, res: any) => {
  res.json({ error: 'invalid request' });
  res.end();
  return;
});



const server = functions.runWith({ maxInstances: 100 }).https.onRequest(app);

export { server as api };

无效json,

{
    "es":"adfasdf"asdf
}

我怀疑这与“此解析由以下正文解析器完成:”https://firebase.google.com/docs/functions/http-events#read_values_from_the_request 相关:

Firebase 函数构建在 functions-framework-nodejs 包之上(或者至少是相同代码的内部变体)。

Within that package, the body parsers you mention are injected. As you correctly surmised, these are indeed added before your code even gets the chance to execute.

由于该错误是 Firebase 操作的内部错误,您需要联系 Firebase support directly