反对一个localhost端口,任何解决方案

时间:2014-09-05 21:25:57

标签: ruby sinatra

我正在使用sinatra与ruby一起工作,当我在终端中运行文件时,sinatra的正常端口是“4567”,但是三天前终端打印这个

     Sinatra/1.4.5 has taken the stage on 4567 for development with backup from WEBrick
     [2014-09-06 00:15:16] INFO  WEBrick::HTTPServer#start: pid=770 port=4567

webrick端口也是“4567”

2 个答案:

答案 0 :(得分:0)

Sinatra Docs

  

:运行 - 启用/禁用内置Web服务器布尔指定   应用程序完全启动后是否启动内置Web服务器   加载。默认情况下,仅在:app_file时启用此设置   匹配$ 0,即直接使用ruby运行Sinatra应用程序文件时   myapp.rb。要禁用内置Web服务器:

set :run, false   
     

:服务器 - 用于内置Web服务器的处理程序。串   或者Rack服务器处理程序名称。当:运行设置是   启用后,Sinatra将运行列表并启动服务器   第一个可用的处理:服务器设置如下设置   默认值:

set :server, %w[thin mongrel webrick]

Sinatra README

  

Sinatra是一款可以轻松地在Ruby中快速创建Web应用程序的DSL:

# myapp.rb
require 'sinatra'

get '/' do
  'Hello world!'
end
Install the gem:

gem install sinatra
And run with:

ruby myapp.rb
View at: http://localhost:4567
  

建议同时运行gem install thin,如果可用的话,Sinatra会选择。{/ p>

默认情况下,Sinatra首先查看您是否安装了瘦服务器软件 - 如果是,Sinatra使用Thin来响应端口4567上的请求。如果您没有安装Thin,则下一个Sinatra会检查您是否拥有安装了Mongrel服务器软件,Sinatra使用Mongrel(如果已安装),最后Sinatra使用随时可用的Webrick服务器软件,它随附ruby。

您看到的消息是Sinatra告诉您它正在使用哪个服务器软件来响应端口4567上的请求。

答案 1 :(得分:0)

很难理解你所看到的问题。也许这会有所帮助:

如果我创建一个包含示例Sinatra代码的简单测试服务器:

require 'sinatra'

get '/hi' do
  "Hello World!"
end

并将其保存到我的桌面test.rb,我可以使用:

运行Sinatra
ruby test.rb

Sinatra告诉我:

== Sinatra/1.4.5 has taken the stage on 4567 for development with backup from Thin
Thin web server (v1.6.2 codename Doc Brown)
Maximum connections set to 1024
Listening on localhost:4567, CTRL+C to stop

如果我卸载Thin:

gem uninstall thin
Remove executables:
  thin

in addition to the gem? [Yn]  y
Removing thin
Successfully uninstalled thin-1.6.2

再次运行代码:

ruby test.rb
[2014-09-05 14:41:23] INFO  WEBrick 1.3.1
[2014-09-05 14:41:23] INFO  ruby 2.1.2 (2014-05-08) [x86_64-darwin13.0]
== Sinatra/1.4.5 has taken the stage on 4567 for development with backup from WEBrick
[2014-09-05 14:41:23] INFO  WEBrick::HTTPServer#start: pid=26786 port=4567

在任何一种情况下,代码都会按照预期执行。我可以使用curl连接并获得“Hello World!”回:

curl http://localhost:4567/hi
Hello World!

Sinatra默认使用Webrick,但会选择其他服务器:

gem install puma
Fetching: puma-2.9.1.gem (100%)
Building native extensions.  This could take a while...
Successfully installed puma-2.9.1
Parsing documentation for puma-2.9.1
Installing ri documentation for puma-2.9.1
Done installing documentation for puma after 3 seconds
1 gem installed

ruby test.rb
Puma 2.9.1 starting...
* Min threads: 0, max threads: 16
* Environment: development
* Listening on tcp://localhost:4567
== Sinatra/1.4.5 has taken the stage on 4567 for development with backup from Puma

gem install thin
Fetching: thin-1.6.2.gem (100%)
Building native extensions.  This could take a while...
Successfully installed thin-1.6.2
Parsing documentation for thin-1.6.2
Installing ri documentation for thin-1.6.2
Done installing documentation for thin after 0 seconds
1 gem installed

ruby test.rb
== Sinatra/1.4.5 has taken the stage on 4567 for development with backup from Thin
Thin web server (v1.6.2 codename Doc Brown)
Maximum connections set to 1024
Listening on localhost:4567, CTRL+C to stop

curl http://localhost:4567/hi
Hello World!

使用-s来控制Sinatra使用的服务器:

ruby test.rb -s puma
Puma 2.9.1 starting...
* Min threads: 0, max threads: 16
* Environment: development
* Listening on tcp://localhost:4567
== Sinatra/1.4.5 has taken the stage on 4567 for development with backup from Puma