json-server 正在运行,但由于 403 而无法访问其 api

Alo*_*ram 3 node.js reactjs json-server

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

"scripts": {
    "server": "json-server --watch db.json --port 5000"
},
Run Code Online (Sandbox Code Playgroud)

在终端输出上运行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...
Run Code Online (Sandbox Code Playgroud)

现在,当我尝试访问 http://localhost:5000/tasks 时,我收到 403 Access to localhost was returned 被拒绝。
我能做什么来解决这个问题?
谢谢!

小智 8

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

原因:蒙特雷的控制中心监听端口 5000 和端口 7000

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

"server": "json-server --watch db.json --port 3000"
Run Code Online (Sandbox Code Playgroud)

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


Alo*_*ram 7

问题出在端口上。把它改成3001就可以了。仍然不明白为什么会在那个端口发生这种情况。