json-server无法通过本地IP访问

问题描述 投票:3回答:4

我正在使用来自npm json-serverhere。它曾经非常适合我的需求:在我的PC上运行服务器并对本地IP(192.168.1.XX)执行GET请求。我重新安装它,现在我只能向localhost或127.0.0.1发出请求。无法再对本地IP(cmd ipconfig)发出请求。我收到这个错误:

enter image description here

正如@fvu提到here

这意味着服务器软件配置为仅侦听localhost接口。这是一个配置项,为了避免暴露可能不安全的服务器,许多服务器程序预先配置为仅在本地主机上侦听。

那么有没有办法通过本地IP访问这个服务器,只要json-server没有一些额外的参数来启用/禁用它?

server ip json-server
4个回答
19
投票

我找到了解决这个问题的方法:

json-server --host 192.168.1.XXX my_file.json

使用此命令,服务器部署在我的本地IP上,Windows要求提供防火墙例外。

另一种解决方案是切换到.NET server - 另一个免费的简单虚假服务器,我可以将本地IP设置为端点。

它需要:

  1. install .NET
  2. 使用CMD命令: git clone https://github.com/ttu/dotnet-fake-json-server.git cd dotnet-fake-json-server/FakeServer dotnet run [--file] [--urls] #like so in my case: dotnet run --file data.json --urls http://192.168.1.192:57602

3
投票

Localhost如果您将使用相同的设备:

json-server --watch filename.json

localhost IP是127.0.0.1所以你可以通过2种方式访问​​这个filename.json

  1. http://localhost:8000/filename.json
  2. http://127.0.0.1:8000/filename.json

如果要从其他计算机/移动设备访问本地主机,请放置计算机的IPV4地址

json-server --host 192.168.0.xx file.json

您还可以使用以下命令分配自己的端口号:

json-server --host 192.168.0.xx file.json --port 4000

然后在任何使用相同网络连接的设备上运行它

  1. http://192.168.0.xx:4000/file.json

最后,如果您不明白我从哪里获取此主机IP,请转到命令提示符> config / all并查找IPv4地址,将该地址复制粘贴到此URL,请记住设备应位于同一网络上以访问此IP 。


1
投票

这是另一种对我有用的方法。如果使用本地LAN IP(例如192.168.1.45)启动json-server,则可以使用IP地址访问服务器(但出于某种原因,不能访问localhost)。例如...

json-server [insert your JSON file] --host [insert your LAN IP]

希望有所帮助!


0
投票

另一个对我有用的选择:

json-server -H 0.0.0.0 -p 3000 -w db.json

注意:您需要通过防火墙为TCP / UDP启用端口3000。像这样的东西

sudo ufw allow 3000
© www.soinside.com 2019 - 2024. All rights reserved.