没有 config.ru 的启动机架服务器?

bea*_*akr 2 ruby rack config

我最近经常使用 Rack,并且想知道如何通过运行文件(例如app.rb)而不使用config.ru. 这是可能的,还是更复杂的方法?

Dyl*_*kow 5

您可以改用内置的 WEBrick 服务器。所以你通常可能会遇到这样的情况:

# app.rb
class App
  def call(env)
    return [200, {"Content-Type" => "text/html"}, "Hello, World!"]
  end
end

# config.ru
require 'app'
run App.new
Run Code Online (Sandbox Code Playgroud)

您可以合并它并ruby app.rb直接运行:

#app.rb
class App
  def call(env)
    return [200, {"Content-Type" => "text/html"}, "Hello, World!"]
  end
end

Rack::Handler::WEBrick.run(App.new, :Port => 9292)
Run Code Online (Sandbox Code Playgroud)

  • 谢谢,但是我已经开始使用更像“Rack::Server.start(...)”的东西,而不是纯粹的 WEBBrick 服务器(也允许您使用 Thin)。谢谢你的提示! (2认同)