简体   繁体   English

json-server db.json 中带有“/”的路径

[英]Path with '/' in json-server db.json

i use server-json to have a fake API, i have the path "playbook/active" in data.json我使用server-json有一个假的 API,我在 data.json 中有路径“playbook/active”

"playbook/active": [{
    "description": "This playbook will install haproxy",
    "name": "Testing playbook 3",
    "tag": [
      "loadbalancer",
      "charge"
    ],
    "path": "/etc/ansible/haproxy.yml",
    "type": "action",
    "id": "4bb107be-9efe-11e9-b3e5-bc5ff4901aa5"
  },
  {
    "path": "google.com",
    "description": "This is the playbook before execution",
    "tag": [
      "webserver",
      "tomcat"
    ],
    "id": "faa746b4-9cb7-11e9-9b94-bc5ff4901aa5",
    "name": "mysql"
  }
]

but i have this error但我有这个错误

Error: Oops, found / character in database property 'playbook/active'.错误:糟糕,在数据库属性“剧本/活动”中找到/字符。

I change to "playbook/active" but same error我改为“剧本/活动”但同样的错误

Check the error message:检查错误消息:

Oops, found / character in database property 'dossier/la'.糟糕,在数据库属性 'dossier/la' 中找到 / 字符。

/ aren't supported, if you want to tweak default routes, see / 不支持,如果你想调整默认路由,请参阅
https://github.com/typicode/json-server/#add-custom-routes https://github.com/typicode/json-server/#add-custom-routes

It seems that slashes are not supported.似乎不支持斜线。

The solution is to create a routes.json file containing the mapping for your url.解决方案是创建一个routes.json文件,其中包含您的 url 的映射。

For example the contents of this file could be:例如,该文件的内容可能是:

{
  my-awesome-endpoint": "playbook/active"
}

For example:例如:

db.json数据库.json

    "list": [
        {
            "name": "abcde",
            "tel": "123454323",
            "id": 5
        }
    ]

routes.json路由文件

{
    "/v1/list?type=hot": "/list"
}

start command:启动命令:

npx json-server --watch db.json --routes routes.json

Providing a complete answer (showcase example)提供完整的答案(展示示例)

Configuring db.json and routes.json can do the trick for you:配置db.jsonroutes.json可以为您解决问题:

  • db.json数据库.json
{
    "playbookActive": [
        {
            "id": 1,
            "name": "Active Playbook 1",
            "description": "Description 1"
        },
        {
            "id": 2,
            "name": "Active Playbook 2",
            "description": "Description 2"
        }
    ]
}

routes.json路由文件

{
    "/playbook/active": "/playbookActive",
    "/playbook/active/:id": "/playbookActive/:id"
}

Note: the mapping in routes.json is goes like this: [expanded/endpoint]: aliasEndpoint where aliasEndpoint should match the one from db.json.注意:routes.json 中的映射是这样的: [expanded/endpoint]: aliasEndpoint其中aliasEndpoint应该与db.json匹配。

package.json包.json

{
    ...
    "scripts": {
        "api": "json-server [path-to-db.json] --routes [path-to-routes.json] --no-cors=false"
    },
    ...
}

Verify the routing on start (logs from npm run api ):在启动时验证路由(来自npm run api日志):

Resources
http://localhost:3000/playbookActive

Other routes
/playbook/active -> /playbookActive
/playbook/active/:id -> /playbookActive/:id

Home
http://localhost:3000

Examples例子

GET → http://localhost:3000/playbook/active GET → http://localhost:3000/playbook/active

Response contains a list with all the active playbooks :响应包含一个包含所有活动剧本的列表

[
  {
    "id": 1,
    "name": "Active Playbook 1",
    "description": "Description 1"
  },
  {
    "id": 2,
    "name": "Active Playbook 2",
    "description": "Description 2"
  }
]

GET → http://localhost:3000/playbook/active/2 GET → http://localhost:3000/playbook/active/2

Response contains the active playbook that matches id=2 :响应包含匹配 id=2 的活动剧本

{
  "id": 2,
  "name": "Active Playbook 2",
  "description": "Description 2"
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM