The purpose of this is just to play around with Rack and see how the middleware can be manipulated for three reasons. One is to better comprehend Rack protocol. The second is to better understand how client/server communication occurs via HTTP. And the third is to understand the sorts of calls and responses I'll be dealing with (e.g. path, headers, and body).
- Create a file for a basic Ruby class that invokes the call method and checks for an environment hash containing a path like
class HelloWorld
def call(env)
if env['PATH_INFO'] == '/hello'
[200, {'Content-Type' => 'text/plain'}, ['Hello World!']]
else
[404, {'Content-Type' => 'text/plain'}, ['Not Found!']]
end
end
end
- Require that newly created file in the config.ru file
require_relative 'hello_world.rb'
- Use rack syntax inside the config.ru file to run the newly created Ruby class
run HelloWorld.rb
- Go to the application directory in terminal
~/user/RackupTestApp
-
Run
$ rackup
which will open a server on port 9292 -
Open local browser, and visit
/hello
to see the successful plain-text response from step 1
- localhost:9292/hello
- In the browser, try visiting any other pages to see that the error message is shown instead
- localhost:9292/foobar