json-server 运行 但由于 403 无法访问其 api

json-server running but can't access its api due to 403

学习reactjs,尝试用json-server
模拟服务器 这是我发送给 运行 服务器的脚本:

"scripts": {
    "server": "json-server --watch db.json --port 5000"
},

运行在终端上安装此 npm run server
输出:

Debugger listening on ws://127.0.0.1:61616/507f7893-92a9-4526-b8f4-a3e71cfa4c62
For help, see: https://nodejs.org/en/docs/inspector
Debugger attached.

> my-app@0.1.0 server
> json-server --watch db.json --port 5000

Debugger listening on ws://127.0.0.1:61629/8a2f877f-f4bd-465e-b454-01a63eabb40a
For help, see: https://nodejs.org/en/docs/inspector
Debugger attached.

  \{^_^}/ hi!

  Loading db.json
  Done

  Resources
  http://localhost:5000/tasks

  Home
  http://localhost:5000

  Type s + enter at any time to create a snapshot of the database
  Watching...

现在当我尝试访问 http://localhost:5000/tasks 时,我收到 403 Access to localhost was denied。
我该怎么做才能解决这个问题?
谢谢!

问题出在端口上。将其更改为 3001 并且有效。 仍然不明白为什么会在那个港口发生这种情况。

也许您正在使用 MacOS Monterey(或者将来可能使用其他版本)。在这里查看原因:https://developer.apple.com/forums/thread/682332

原因:Monterey 上的控制中心侦听端口 5000 和端口 7000

修复:更改端口以避免范围 5000-7000,如下所示,

"server": "json-server --watch db.json --port 3000"

或者只删除自定义参数 --port 3000 因为这是默认值