Building a Simple Single-Page Web Server in Ruby

Building a Simple Single-Page Web Server in Ruby

Imagine crafting a web server in just a few lines of Ruby code! Let's explore how to create a basic single-page web server using Ruby's socket library.

Interactive Q&A: Exploring the Basics

Q: What library does Ruby offer for handling low-level networking operations? A: The socket library.

Q: What does TCPServer.new('localhost', 8080) in Ruby do? A: It initializes a TCP server on the localhost at port 8080.

Embracing Ruby's Socket Magic

With the power of the socket library, you can craft a minimalist web server in Ruby:

require 'socket'

server = TCPServer.new('localhost', 8080)

loop do
  client = server.accept

  request = client.gets

  puts "Request: #{request}"

  response = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n<html><body><h1>Hello, World!</h1></body></html>"

  client.puts response

  client.close
end

Exploring the Server Logic

This code snippet initializes a TCP server on localhost at port 8080. It enters an infinite loop to continuously handle incoming client connections.

  • Upon connection, it retrieves the client's request.

  • It responds with a simple HTTP/1.1 200 OK response containing a basic HTML page displaying "Hello, World!".

Running the Server

  1. Save the Code: Store this code in a file, say simple_server.rb.

  2. Run the Server: Open your terminal and execute ruby simple_server.rb.

Accessing Your Single-Page Site

Open a web browser and visit http://localhost:8080. You'll behold the magic of your Ruby-built web server displaying a "Hello, World!" message.

Conclusion: Crafting a Basic Web Server in Ruby

With minimal code, Ruby's socket library empowers you to create a functioning web server. This simple example showcases the essence of handling incoming requests and serving responses—all within Ruby!