json-server为特定URI返回404

gcxthw6b  于 2022-12-15  发布在  其他
关注(0)|答案(1)|浏览(183)

我使用json-server,代码如下。
db.json

{
  "en-boards": [
    {
      "id": 0,
      "name": "News",
      "uri": "/board/en-news"
    },
    {
      "id": 1,
      "name": "Politics",
      "uri": "/board/en-politics"
    }
  ],
  "board-en-news-threads-latest": [
    {
      "id": 0,
      "poster": null,
      "zero-post": "This is a thread 0.",
      "attachment-path": null,
      "post-number": 100,
      "utc-timestamp": "2022-01-01T00:00:00"
    },
    {
      "id": 1,
      "poster": null,
      "zero-post": "This is a thread 1.",
      "attachment-path": null,
      "post-number": 100,
      "utc-timestamp": "2022-01-01T00:00:00"
    }
  ]
}

routes.json

{
  "/api/*": "/$1",
  "/api/en-boards": "en-boards",
  "/api/board/en-news/threads/latest": "board-en-news-threads-latest"
}

我运行json-server --watch db.json --routes routes.json --middlewares middleware.js --port 4000
当我运行curl -i http://localhost:4000/api/en-boards时,我得到200 OK,但当我运行curl -i http://localhost:4000/en-news/threads/latest时,我得到404 Not Foundwget和React应用程序的行为方式相同。

环境

  • MacOS 13.0操作系统
  • 节点. js 18.11.0
  • json服务器0.17.0
  • curl 度7.84.0
  • 工作1.21.3
  • React18.2.0

谢谢你的阅读。任何帮助都将不胜感激。

6jjcrrmo

6jjcrrmo1#

以前也遇到过类似的情况...
在我的例子中,我需要像基URL(1)和端点路径(2)这样的东西,如下所示-x1c 0d1x

{
"/api/board/*": "/$1",
"/api/*": "/$1",
"/en-boards": "/en-boards",
"/en-news/threads/latest": "/board-en-news-threads-latest"}

这将满足以下端点的需求

http://localhost:3000/api/board/en-news/threads/latest
http://localhost:3000/api/en-news/threads/latest
http://localhost:3000/en-news/threads/latest

http://localhost:3000/api/board/en-boards
http://localhost:3000/api/en-boards
http://localhost:3000/en-boards

注意:要将 /API/board/en-news/threads/latest 路由到 /board-en-news-threads-latest,需要保持基本URL的顺序(即)

"/api/board/*": "/$1",
"/api/*": "/$1",

希望这有帮助:)

相关问题