无法在浏览器中打开 Swagger API

Cannot open Swagger API in browser

我正在尝试在 VM/Vagrant 框中设置 Swagger。我所做的如下。

我。 端口转发 使用 Vagrantfile

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

二。 流浪盒中的 Swagger 配置

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

我尝试使用命令 swagger project edit -p 3000 -s 打开 Swagger 编辑器,它给出

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

似乎一切都很完美所以只访问了本地桌面的浏览器(windows),然后访问了http://localhost:3000/editor/spec,这让我无法访问该站点.

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

我在这里做错了什么?

问题是

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

所以除了你的本地主机之外,它无法通过任何网络接口访问。

您需要更改此设置以在服务器的 IP 上启动应用程序,或者您可以使用 0.0.0.0(特殊 IP,以便所有接口都可以访问它)

您需要 运行 为

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

(参见 the CLI reference guide)- 然后您将能够从位于 http://localhost:3000

的主机访问

注意:另一种选择是使用 private network or public network 分配静态 IP;然后启动 swagger project edit --host <static_ip_from_vagrantfile> -p 3000 -s 并使用静态 IP 从您的主机访问,在这种情况下,您不需要将任何端口从来宾转发到主机。