Search code examples
firebasegoogle-cloud-functionsfirebase-hosting

how to handle root path request with node.js on firebase hosting?


I'm developing web system using firebase hosting + functions. Inspite of specifying rewrite rules on firebase.json, a part of routing doesn't work.

root/
 ├ functions/
 │  ├index.js
 │  ├routes/
 │  │  └ index.js
 │  └views/
 │    ├ index.jade
 │    └ sub.jade
 └ public/
    └index.html // this is created by default. I don't want to use this.

This is my firebase.json

"rewrites": [{
      "source": "**",
      "function": "app"
    }],

And this is node.js code.

router.get('/', function(req, res, next) {
    res.render('index');
});

router.get('/subdir', function(req, res, next) {
    res.render('sub');
});

result

https://myurl/ -> public/index.html is shown.(not handled on node.js)
https://myurl/ -> handled on node.js

Do you know how to handle root path request with node.js on firebase hosting.


Solution

  • See https://firebase.google.com/docs/hosting/full-config#hosting_priority_order .

    Priority order of Hosting responses

    The different Firebase Hosting configuration options described on this page can sometimes overlap. If there is a conflict, Hosting determines its response using the following priority order:

    1. Reserved namespaces that begin with a /__/* path segment
    2. Configured redirects
    3. Exact-match static content
    4. Configured rewrites
    5. Custom 404 page
    6. Default 404 page

    Exact-match static content is high priority order than Configured rewrites.

    So https://myurl/ get static content /index.html.

    Can you try remove public/index.html?


    I tried. I can rewrite.

    See https://github.com/zkohi/firebase-hosting-using-functions-samples .

    And you should check https://firebase.google.com/docs/hosting/functions .