代码之家  ›  专栏  ›  技术社区  ›  Jordan Williams

json服务器“拒绝访问本地主机”

  •  0
  • Jordan Williams  · 技术社区  · 4 年前

    我刚开始使用json服务器,并创建了一个脚本“npm-run-server”来构建它。然而,当我试图在浏览器中访问json时,我得到了“拒绝访问localhost”。

    这是脚本:

    json-server --watch db.json --port 5000
    

    enter image description here

    0 回复  |  直到 4 年前
        1
  •  6
  •   Josh Scanlan    4 年前

    我对5000端口也有同样的问题。我认为这可能是因为端口已经被其他东西使用了(如果你在Mac上,我相信它是从蒙特利开始的)。我把它换到另一个端口,它起作用了。

        2
  •  2
  •   redOctober13    4 年前

    这是由于Mac操作系统蒙特利的AirPlay接收器使用端口5000。更多详细信息,请访问 this answer .

    您可以禁用AirPlay(系统首选项-->共享),也可以在 package.json 如前所述:

    --port 3000

        3
  •  1
  •   dmoncado    4 年前

    同样,我只是将json脚本中的端口标志更改为3000,重新运行服务器,一切都很好。

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