create a http server with a specific route in ruby

Here's a simple example of creating an HTTP server with a specific route in Ruby using the WEBrick library:

main.rb
require 'webrick'

# Define a server
server = WEBrick::HTTPServer.new(Port: 3000)

# Handle a specific route
server.mount_proc '/hello' do |req, res|
  res.content_type = 'text/plain'
  res.body = 'Hello, world!'
end

# Start the server
server.start
250 chars
14 lines

This code creates a server on port 3000 and specifies a route /hello which responds with a simple "Hello, world!" message. When you start the server and visit http://localhost:3000/hello in your web browser, you should see the message displayed.

gistlibby LogSnag