Objective: I want Express
to return /register
when the browser URL is http://localhost:5000/register
. This seemingly simple goal is proving to be a challenge with Express
.
Let's start by looking at my 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"
}
]
}
Next, in my Firebase Functions file, index.js:
const functions = require('firebase-functions');
const express = require("express");
const site = express();
// Handling paths not starting with /app/.
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);
Here are the outputs from the console:
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 = /
Despite various attempts resulting in similar outcomes like /
, none seem to accomplish my straightforward goal of obtaining just /register
without manipulating
/firebase-project-id/us-central1/site/register
. Are there any other dependable approaches that can help me achieve this? Appreciate your suggestions!