【问题标题】:Firebase Functions With Express Failed to decode param %CO带有 Express 的 Firebase 函数无法解码参数 %CO
【发布时间】:2021-12-19 14:43:35
【问题描述】:

在使用 app.listen(8080) 时,不使用 Firebase here 可以解决此错误,但是在使用 exports.app = functions.https.onRequest(app) 提供云功能时这不起作用

这是一个简单的复制代码

const app = express();
app.get('**', (req, res) => res.send('working'));
app.use((err, req, res, next) => res.redirect('/404'));
exports.app = functions.https.onRequest(app) // doesnt work
// app.listen(5000) // works

如何在 firebase 函数中发现这个错误?我希望重定向工作。

firebase 测试:firebase serve --only functions

快递服务:node index.js

要测试的网址:http://localhost:5000/%CO

请注意,附加的%CO 是无法通过express 解码的。使用 express 方法而不是 firebase 函数方法时会捕获此错误。

由于这似乎是一个错误,我还创建了一个issue here on github,以防我找不到解决方法。

【问题讨论】:

  • 我检查了 firebase 日志,发现了错误发生的原因,但我还不知道该怎么做。 AppData\Roaming\npm\node_modules\firebase-tools\node_modules\express\lib\router\layer.js:172:12) 在 express 收到请求之前捕获 decodeURIComponent(val) 错误。我怎样才能覆盖它?

标签: node.js firebase express google-cloud-functions


【解决方案1】:

尝试在exports. 中更改app 的名称

exports.newApp = functions.https.onRequest(app) 

【讨论】:

  • 抱歉不是这个原因,导出的变量不能影响局部变量
【解决方案2】:

根据documentation,使用带有 Firebase 函数的 Express 应用程序的正确方法是将应用程序传递给如下函数:

// Expose Express API as a single Cloud Function:
exports.app = functions.https.onRequest(app);

通过 Firebase Functions 运行时,not apply 会监听请求端口。至于如何在将您的应用程序传递给函数时捕获错误,就我所知reviewed 而言,它的完成方式与您引用的问题相同。您可以在使用Cloud Functions 的同时使用Express middleware 来捕获错误。

此外,related question 解释了使用 Firebase 函数实现重定向,它使用 documentation 通过修改 firebase.json 文件来配置如何重定向。

【讨论】:

  • 感谢您的回答,由于您的回答没有涉及我面临的问题,因此我已对问题进行了编辑以使其更清楚。服务有效,但快速中间件在使用 firebase 时不起作用
  • 我在帖子下阅读了您的评论,基于此相关thread,您似乎处于类似情况。您应该创建应用程序中间件来捕获错误并将用户重定向到您想要的页面,而不是使用错误处理程序来捕获错误。我建议您遵循有关 routing middleware 的指南。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-07-06
  • 1970-01-01
  • 2022-12-14
  • 2023-03-25
相关资源
最近更新 更多