json-server cannot access via local IP

吃可爱长大的小学妹 提交于 2019-11-30 05:25:57

I found the solution for this issue:

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

Using this command, server is deployed on my local IP, and Windows asks for a firewall exception.

Another solution is to switch to .NET server - another free simple fake server where I can setup local IP as endpoint.

All it needs to:

  1. install .NET
  2. use CMD commands:

       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
    

Localhost if you will use the same device:

json-server --watch filename.json

the localhost IP is 127.0.0.1 so, you can access this filename.json by 2 ways, either by

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

In case you want to access the localhost from another computer/mobile device, place the IPV4 address of your computer

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

You can also assign a port number of your own using this command:

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

then run it on any device connected with the same network using

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

Finally if you did not understood where did i take this host IP from, Go to command prompt > config /all and look for IPv4 address, copy paste that address to this URL, remember that devices should be on the same network to access this IP.

Another option that worked for me:

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

Note: You need to enable port 3000 for TCP/UDP through firewall though. Something like this

sudo ufw allow 3000

Here's another method that worked for me. If you start json-server using your local LAN IP (e.g. 192.168.1.45), you can access the server using an IP address (but not localhost for some reason). For example...

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

Hope that helps!

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!