测试客户的ApiService
类我需要用模拟替换我的真实后端 URL,为此我选择了 json 服务器 .我设置了一个代理配置来转发所有以 http://localhost:4200/v1/api
开头的请求至 http://localhost:3000
:
{
"/api/v1": {
"target": "http://localhost:3000",
"secure": false
}
}
当我发送类似
http://localhost:4200/api/v1/users
的请求时它会起作用但是 不适用于嵌套端点(例如 http://localhost:4200/api/v1/auth/token
)。我发现 json 服务器 不支持对嵌套对象的请求,所以我更改了 数据.json 如下:{
"auth_token": {
"access_token":"accesstoken1",
"token_type":"Bearer"
}
}
并成立路线.json 为 json 服务器 :
{
"/auth/token": "/auth_token"
}
但是通过 还是不行json 服务器 用途 路线.json :
[0] Other routes
[0] /auth/token -> /auth_token
我究竟做错了什么?
最佳答案
由于我的错误和 ,我的解决方案不起作用json 服务器 魔法:
正如我发现的 json 服务器 替换 /api/v1
至 /
自动地。但是如果你使用上面的路由设置,那么 /api/v1/auth/token
不会被替换为 /api/v1/auth_token
自动,您将获得 404 错误。
所以 json 服务器 行为给我造成了一种错觉,即 中的代理设置| Angular 替换 http://localhost:4200/api/v1
至 http://localhost:3000
.因此,在我的情况下,正确的路由如下所示:
{
"/api/v1/auth/token": "/api/v1/auth_token"
}
关于angular - json-server 中嵌套端点的自定义路由,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59497251/