vagrantswaggerlocalswagger-editordefault-browser

Cannot open Swagger API in browser


I am trying to set up Swagger in a VM/Vagrant box. What I have done is below.

i. Port Forwarding using Vagrantfile

dev.vm.network "forwarded_port", guest: 3000, host: 3000, host_ip: 
"127.0.0.1", auto_correct: true

ii. Swagger configuration in a vagrant box

npm install -g swagger
Create a new swagger project
swagger project create hello-world

I tried to open Swagger Editor with command swagger project edit -p 3000 -s, which gives

Starting Swagger Editor. Running Swagger Editor API server. You can make GET and PUT calls to http://127.0.0.1:3000/editor/spec

Seems like everything is perfect so just visited browser of local desktop(windows), and visited http://localhost:3000/editor/spec which gives me This site can’t be reached.

curl http://localhost:3000/editor/spec is working fine in the VM/vagrant box.

What wrong am I doing here?


Solution

  • The issue is

    calls to http://127.0.0.1:3000/editor/spec

    so its not accessible on any network interface other than your localhost.

    You need to change this to get started the app on the IP of the server or you can use 0.0.0.0 (special IP so all interfaces can access it)

    You need to run as

    swagger project edit --host 0.0.0.0 -p 3000 -s 
    

    (See the CLI reference guide) - Then you will be able to access from your host at http://localhost:3000

    NB: the other option is to assign a static IP either with private network or public network; then start swagger project edit --host <static_ip_from_vagrantfile> -p 3000 -s and access from your host with the static IP and in such case you dont need to forward any port from the guest to the host.