37

I'm using npm json-server from here. It used to work great for my needs: run a server on my PC and do GET requests to local IP (192.168.1.XX). I reinstalled it and now I can do requests only to localhost or 127.0.0.1. Can't do requests to local IP (cmd ipconfig) anymore. I'm getting this error:

enter image description here

As @fvu mentioned here

this means that the server software is configured to listen on the localhost interface only. This is a configuration item and to avoid exposing a potentially unsecure server many server programs come preconfigured to listen on localhost only.

So is there a way to access this server via local IP as long as json-server doesn't have some extra parameters to enable/disable it?

Choletski
  • 7,074
  • 6
  • 43
  • 64
  • this solution doesn't work for me. [this issue for me has been solved with this solution](https://stackoverflow.com/questions/57304410/i-cant-access-to-data-in-json-server-from-react-native-with-use-of-fetchs-meth/57324994#57324994) –  Aug 04 '19 at 06:21

8 Answers8

86

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
    
Choletski
  • 7,074
  • 6
  • 43
  • 64
19

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.

Harshit Pant
  • 1,032
  • 11
  • 14
9

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
PravyNandas
  • 607
  • 11
  • 12
4

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!

4

My fake json-server was running on vagrant machine as 192.168.100 and I am using React app to consume it and faced the same issue so I run this command on my vagrant machine (guest OS) and everything works fine after this. i.e. I can access my APIs something like this

192.168.1.100/tasks


npx json-server --watch db.json --port 8000 -H 192.168.1.100

Here db.json is my data file consisting of JSON port is 8000 host is 192.168.1.100 which is the IP address of my server/vagrant machine

AND don't forget to allow port on the firewall (Linux)

sudo ufw allow 8000
ajay_full_stack
  • 494
  • 7
  • 13
1

Type this in your command line:

npx json-server <folder/filename> --port <whatever port you want>

I used this, for example:

npx json-server api/db.json --port 3000 
colidyre
  • 4,170
  • 12
  • 37
  • 53
toeyeen
  • 41
  • 3
1

Use --host 0.0.0.0 it will get your local IPV4 automatically AND -p "SpecifcPort"

0

In my case

npx json-server --watch fileName --port YourPort

npx json-server --watch db.json --port 3000

  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Oct 05 '22 at 05:51