I'm having this issue when try to get /public folder with express in firebase.
My project tree are:
ProjectRoot
├───functions -> index.js
├───node_modules
├───public -> index.html
└───firebase.json
Im trying to get /public/index.html from /functions/index.js.
This is my index.js
const functions = require('firebase-functions');
const express = require("express");
var path = require('path');
const app = express();
// Static files
app.use("/public", express.static(path.join(__dirname , '/..' , "/public")));
// Routes
// Test1 is actually working
app.get("/test1", (req, res) => {
try{
res.send('🔥Module 1 is on fire🔥');
}catch(error){
console.error("Error Debug: "+ error);
}
});
// Test2 isn't working
app.get("/test2", (req, res) => {
try{
res.sendFile('/public/index.html');
}catch(error){
console.error("Error Debug: "+ error);
}
});
// Test3 isn't working
app.get("/test3", (req, res) => {
try{
res.sendFile(path.join(__dirname,'/..','/public/index.html'));
}catch(error){
console.error("Error Debug: "+ error);
}
});
exports.app = functions.https.onRequest(app);
The console throw me chronologically
Function execution took 6 ms, finished with status code: 200 --> Test1 Works
ENOENT: no such file or directory, stat '/public/index.html' Test2 code 404
ENOENT: no such file or directory, stat '/public/index.html' Test3 code 404
And finally my firebase.json are
{
"hosting": {
"public": "public",
"ignore": [
"firebase.json",
"**/.*",
"**/node_modules/**"
],
"rewrites": [
{
"source": "**",
"function": "app"
}
]
},
"functions": {
"source": "/functions",
"runtime": "nodejs16"
}
}