create a http server with a options route in ruby

main.rb
require 'webrick'

# create http server
server = WEBrick::HTTPServer.new(Port: 8000)

# define options route
server.mount_proc '/options' do |req, res|
  res['Allow'] = 'GET, POST, OPTIONS'
  res.status = 200
end

# start http server
server.start
247 chars
14 lines

In the above code snippet, we first require the webrick library which provides an HTTP server implementation in Ruby. Then, we create a new instance of WEBrick::HTTPServer class which listens to port 8000.

Next, we define a new route for /options by using server.mount_proc method. In this route, we set the Allow header to specify which HTTP methods are allowed in the response.

Finally, we start the HTTP server by calling the server.start method. Now, the server is ready to accept incoming HTTP requests on port 8000.

gistlibby LogSnag