Express.js:在 Firebase 函数中使用带有 RegEx 路径模式的 app.use() 时如何获得 'Clean Path'?

Express.js: How to get 'Clean Path' when using app.use() with RegEx path pattern in Firebase Functions?

我的目标:当浏览器URL为http://localhost:5000/register时,我只想Express到return/register,仅此而已。但似乎 Express.

无法轻易实现如此简单的目标

首先,我认为在继续之前向您展示我的 firebase.json 是件好事:

firebase.json:

"hosting": 
    {
      "target": "store",
      "public": "store/public",
      "ignore": [
        "firebase.json",
        "**/.*",
        "**/node_modules/**"
      ],
      "rewrites": [
        {
          "source": "/app{,/**}",
          "destination": "/app/app.html"
        },
        {
          "source": "**",
          "function": "site"
        }
      ]
    }

然后在我的 Firebase 函数中,index.js:

const functions = require('firebase-functions');
const express = require("express");
const site = express();

// Any path except /app/anything.
site.use(/^\/(?!app).*/, function (req, res, next) {
    console.log(`req.path] = ${req.path}`);
    console.log(`req.route.path] = ${req.route.path}`);
    console.log(`req.originalUrl] = ${req.originalUrl}`);
    console.log(`req.url] = ${req.url}`);
    console.log(`req.baseUrl] = ${req.baseUrl}`);
    console.log(`site.mountpath = ${site.mountpath}`);

    next();
});

site.get("/", (req, res) => {
    res.send("Homepage");
});

site.get("/register", (req, res) => {
    res.send("Registration Page");
});

exports.site = functions.https.onRequest(site);

控制台结果如下:

i  functions: Beginning execution of "site"
>  req.path] = /
>  req.route.path] = *
>  req.originalUrl] = /firebase-project-id/us-central1/site/register
>  req.url] = /
>  req.baseUrl] = /firebase-project-id/us-central1/site/register
>  site.mountpath = /

如您所见,上面的 none 可以帮助实现我的简单目标,除了具有相似结果的许多变体,例如/。有没有其他可靠的选项我可以使用它来获取 /register 而不会危险地操作 /firebase-project-id/us-central1/site/register 字符串?非常感谢!

参考 req.headers['x-original-url'] 有效!

控制台输出:

req.headers['x-original-url'] = /register