This morning, I was trying to learn Rack and how Rails uses it. So I created a new gem using the bundle gem
command. Then I added a config.ru
file with the following code.
class App
def call(env)
['200', {'Content-Type' => 'text/html'}, ['Hello World']]
end
end
run App.new
# Alternatively, you can use this, too
# run Proc.new { |env| ['200', {'Content-Type' => 'text/html'}, ['Hello World']] }
To launch the server, I ran the rackup
command. However, it immediately crashed with the following error.
➜ sandbox (main) ✗ rackup
bundler: failed to load command: rackup (/../gems/ruby-3.0.2/bin/rackup)
/../lib/rack/handler.rb:45:in 'pick': Couldn't find handler for: puma, thin, falcon, webrick. (LoadError)
This error means that Rack can’t find an application (app) server to serve your web application.
To fix this error, add an app server to your application. For example, to install Puma (the default app server that ships with Rails), you will add this to your Gemfile.
# Gemfile
gem 'puma'
Then in the terminal, run the following command.
➜ bundle install
That’s it. If you run the rackup
command, it will start the server without errors.
➜ sandbox (main) ✗ rackup
Puma starting in single mode...
* Puma version: 5.5.2 (ruby 3.0.2-p107) ("Zawgyi")
* Min threads: 0
* Max threads: 5
* Environment: development
* PID: 2378
* Listening on http://127.0.0.1:9292
* Listening on http://[::1]:9292
Use Ctrl-C to stop
Hope this helps.