ember.js - 从文件和 Ember CLI Mirage 中获取内容
问题描述
我的 Ember 应用程序中有一个组件,它从静态降价文件返回数据。
didInsertElement() {
fetch('markdown/faqs-content.md').then(response => {
response.text().then(result => {
this.set('markdown', result);
});
});
}
当应用程序运行时,请求转到http://localhost:4200/markdown/faqs-content.md
并且一切正常。
但是,在海市蜃楼中,我收到此错误:
Mirage: Your app tried to GET '/markdown/faqs-content.md', but there was no route defined to handle this request. Define a route for this endpoint in your routes() config. Did you forget to define a namespace?
即使以下内容包含在以下内容中,也会发生上述情况mirage/config.js
:
this.passthrough();
据推测,这是因为请求 URL 中不包含 API 命名空间。
可以通过将完整的 URL 添加到 passthrough 来解决该问题:
this.passthrough('https://localhost:4201/markdown/faqs-content.md');
这样做有两个问题:
- 我并不总是知道应用程序所在的端口,并且
- 我不想失去 using 的功能
this.passThrough()
,它会自动为任何 API 请求调用 pass through,如果mirage/config.js
.
因此,我有 2 个问题。
在
config.js
中,有没有办法获取 Ember 服务器正在运行的端口,以允许类似的东西https://localhost:${port}/markdown/faqs-content.md
?有没有一种方法可以将 Mirage 配置为传递给 的请求
https://localhost:4201/markdown/faqs-content.md
,并且仍然允许它传递给没有定义相应路由的任何其他请求?
解决方案
直通(以及一般的路由 API)有一些不幸的奇怪行为,基于它的编码方式 + 它用来拦截请求的底层库。我们需要更新指南,因为其他人已经遇到了这个问题。
如果你把
this.namespace = 'api'
this.passthrough()
那么 Mirage 将让所有请求通过/api/*
,但如果您将调用移至passthrough
之前,或者将命名空间“重置”为空字符串,它应该处理对当前域的所有请求。
我需要测试,但我会尝试两者
this.namespace = ''
this.passthrough()
和
this.passthrough('/**')
看看他们是否工作。(我们真的需要向 Mirage 添加一个嵌套路由 DSL!它会删除所有这些笨拙的 API。)
推荐阅读
- python - 为什么生成器函数内部的命令不执行?
- amazon-web-services - 在 AWS 服务上运行抓取脚本
- python - UndefinedTable:关系不存在
- html - 带有框架的烧瓶wtf
- python - 在熊猫中,如何从另一个数据框的比例创建一个新的数据框?
- ruby-on-rails - 嵌入式 javascript,将表单数据发送回应用程序。路由错误
- kubernetes - 使用 dns 质询向 AKS PublicIP (AzureDNS) 颁发证书
- android - 如何在 Java 中以编程方式找到蓝牙版本?
- node.js - Mongo DB中的图像文件作为URI
- angular - 如何在 matColumnDef 中为动态表 ngfor?