首页 > 解决方案 > 如何在firebase托管上使用node.js处理根路径请求?

问题描述

我正在使用 Firebase 托管 + 功能开发网络系统。尽管在 firebase.json 上指定了重写规则,但部分路由不起作用。

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.

这是我的 firebase.json

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

这是 node.js 代码。

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

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

结果

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

你知道如何在 firebase 主机上使用 node.js 处理根路径请求吗?

标签: firebasegoogle-cloud-functionsfirebase-hosting

解决方案


请参阅https://firebase.google.com/docs/hosting/full-config#hosting_priority_order

托管响应的优先顺序

此页面上描述的不同 Firebase 托管配置选项有时可能会重叠。如果存在冲突,Hosting 将使用以下优先级顺序确定其响应:

  1. 以 /__/* 路径段开头的保留命名空间
  2. 配置的重定向
  3. 完全匹配的静态内容
  4. 配置的重写
  5. 自定义 404 页面
  6. 默认 404 页面

完全匹配静态内容的优先级高于已配置的重写。

所以https://myurl/获取静态内容/index.html

您可以尝试删除 public/index.html 吗?


我试过了。我可以重写。

请参阅https://github.com/zkohi/firebase-hosting-using-functions-samples

你应该检查https://firebase.google.com/docs/hosting/functions


推荐阅读