'Firebase cloud functions CAN'T find static files in public folder
- project
- stripejs
- functions
index.js
public
- success.html
- functions
- stripejs
It's been hours I'm trying to redirect an API ( using cloud functions) to my html page (inside the public folder).
router.get('/finishOnboarding', async (req,res) => {
let content = path.resolve('../public/success.html')
res.sendFile(content);
});
I tried multiples things.
put the public folder Outside of the functions folder, then i noticed i need to nest it inside because when deploying it need to be accessed in google cloud
using
__dirname
+ absolute path
No matters what I do, When I'm checking the Functions logs I always got the same error :
Error: ENOENT: no such file or directory, stat '/public/success.html'
If I'm trying to redirect using
path.join(__dirname, '/public/success.html');
I got :
Error: ENOENT: no such file or directory, stat '/workspace/routes/public/success.html'
firebase.json is configured as needed
{
"hosting": {
"public": "functions/public",
"ignore": [
"firebase.json",
"**/.*",
"**/node_modules/**"
],
"rewrites": [
{
"source": "**",
"function": "app"
}
]
},
"emulators": {
"hosting": {
"port": 5003
}
}
}
Where is the problem coming from? I'm sure its a simple problem with the paths, but Im very confused..
Solution 1:[1]
Cloud Functions and Hosting run on completely separate infrastructure, so you can't reach from one to the other with a path
type call.
If you want to redirect the user, you'll need to send a redirect instruction on the response. Doing that means the browser will get the instruction to request a different URL, and that request will then be served by Firebase Hosting.
If you want to rewrite the response (so serve HTML from a different location within your code), you will need to have those files inside (or under) the functions
folder:
- project
- stripejs
- functions
index.js
public
- success.html
- functions
- stripejs
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Frank van Puffelen |