I am using Google cloud functions with express js to serve up some HTML files. My js files do not show up in the browser. It says 404 not found.
My folder structure
My HTML file:
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
<script type="module" src="/public/js/auth.js"></script>
</head>
<body>
<div class="content__container">
<h1>Hello world</h1>
</div>
<script src="/public/js/main.js"></script>
</body>
</html>
My app.js
const functions = require("firebase-functions");
const express = require("express");
const path = require("path");
const app = express();
app.use("/js", express.static(path.join(__dirname, "public/js")));
exports.admin = functions.https.onRequest((req, res) => {
res.sendFile(path.join(__dirname, "public", "admin.html"));
});
The error in the browser when I invoke the cloud function admin
Functions directory
I think you forgot to declare app in your js file
const app = express();
In your HTML file:
<script src="./js/main.js">
If js is your static/public dir:
<script src="./main.js">
Also, in your js file:
res.sendFile(path.join(__dirname, "admin.html"));
From where have you exposed the Express app? There must be a HTTP function that should look something like this:
const app = express()
app.use("/js", express.static(path.join(__dirname, "public/js")));
exports.api = functions.https.onRequest(app);
// This is missing? or you are not requesting at this URL
When you add the above function, your JS file should be available at the following URL:
'http://localhost:5001/photography-5b191/us-central-1/api/js/main.js'
// notice the function name ('api' in this case) ---->^^^
The function name that has exposed the Express app is missing from the URL in the network log that you've shared.