[英]Is there a way to normalise differences in request.url when http firebase-functions running under emulator and deploy
For firebase tools 6.9.2: 对于Firebase工具6.9.2:
When using firebase emulators:start --only functions, hosting
where the functions are invoked via a hosting rewrite rule, the functions are invoked with different request url path prefixes than if the functions were deployed to the cloud. 当使用
firebase emulators:start --only functions, hosting
通过托管重写规则调用功能的托管时,与将功能部署到云中相比,使用不同的请求url路径前缀来调用功能。 For example given a firebase.json hosting snippet such as: 例如给定的firebase.json托管代码段,例如:
{
"target": "myapi",
"public": "./dist/hosting/myapi",
"rewrites": [{
"source": "/api/myapi/*",
"function": "myapi"
}
]
},
When deployed to firebase, the myapi
function will be called with a request url that starts with: /api/myapi/
部署到
myapi
将使用以/api/myapi/
开头的请求网址来调用myapi
函数。
but if run in the emulator it ends up looking like: /[firebase-project-id]/[firebase-region]/myapi/api/myapi/
但如果在模拟器中运行,则最终看起来像:
/[firebase-project-id]/[firebase-region]/myapi/api/myapi/
This doesn't make a difference if the function doesn't look at the request path but in my case, the function is a handler for an express (koa) handler with routes so knowing the base path is important. 如果该函数不查看请求路径,那么这没有什么区别,但就我而言,该函数是具有路由的Express(koa)处理程序的处理程序,因此了解基本路径很重要。
I've looked to see if anything is available in process.env but only the GCLOUD_PROJECT name and a few FIREBASE_CONFIG params are there. 我一直在查看process.env中是否有任何可用的东西,但是那里只有GCLOUD_PROJECT名称和一些FIREBASE_CONFIG参数。
I could not find any documentation on more advanced control of the rewrite to a function in firebase.json. 我找不到关于在Firebase.json中重写功能的更高级控制的任何文档。
I can work around it in testing by setting an environment variable to match the prefix the functions are running at locally and taking it into account during request handling. 我可以在测试中通过设置环境变量来使其与功能在本地运行的前缀匹配并在请求处理期间将其考虑在内来解决该问题。 Unfortunately the request url prefix is not available until after the emulators are started and the function urls are logged.
不幸的是,直到启动仿真器并记录功能url后,请求url前缀才可用。
I am hoping to find a better solution for testing. 我希望找到更好的测试解决方案。
This turned out to be a bug and is was resolved for my case in firebase-tools 6.10.0. 原来这是一个错误,在firebase-tools 6.10.0中针对我的情况已解决。
https://github.com/firebase/firebase-tools/issues/1279 https://github.com/firebase/firebase-tools/issues/1279
This related issue was also opened: 此相关问题也已打开:
https://github.com/firebase/firebase-tools/issues/1312 https://github.com/firebase/firebase-tools/issues/1312
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.