firebase - 如何在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 处理根路径请求吗?
解决方案
请参阅https://firebase.google.com/docs/hosting/full-config#hosting_priority_order。
托管响应的优先顺序
此页面上描述的不同 Firebase 托管配置选项有时可能会重叠。如果存在冲突,Hosting 将使用以下优先级顺序确定其响应:
- 以 /__/* 路径段开头的保留命名空间
- 配置的重定向
- 完全匹配的静态内容
- 配置的重写
- 自定义 404 页面
- 默认 404 页面
完全匹配静态内容的优先级高于已配置的重写。
所以https://myurl/
获取静态内容/index.html
。
您可以尝试删除 public/index.html 吗?
我试过了。我可以重写。
请参阅https://github.com/zkohi/firebase-hosting-using-functions-samples。
推荐阅读
- asp.net-mvc - 我应该如何解释这个 MiniProfiler 结果?
- apache-kafka - 如何使用 KTable 从 kafka 1.1.0 获取最新值
- javascript - 如何将 Alert 替换为 navigation.navigate('Home")? like Open the new screen in catch
- java - Java多线程程序卡住等待
- vuetify.js - vuetify中是否有没有任何html模板的独立警报功能?
- python - 如何在 python GUI 中显示 cv2 视频?
- rstudio - R中的####(四个标签)是什么意思?
- ios - 无法从 Swift 代码中读取自定义 iOS 目标属性键值
- mysql - 生成 10 个随机 id 数字形成字符串中的表
- docker - 不能让 docker-compose 像 dockerfile 一样工作