# Building a Simple Web Server with Ruby 2.0+

DevFeed: [Building a Simple Web Server with Ruby 2.0+](<https://devfeed.tech/articles/building-a-simple-web-server-with-ruby-2-0-21045.md>)

Original publisher: [Read original article](<https://jakeyesbeck.com/2015/10/11/building-a-simple-web-server-with-ruby-2/>)

Published: 2015-10-11T12:00:00Z

Content type: tutorial

Language: en

Sources: [Jake Yesbeck](<https://devfeed.tech/sources/jake-yesbeck.md>)

Topics: [Ruby](<https://devfeed.tech/topics/ruby.md>), [servers](<https://devfeed.tech/topics/servers.md>), [HTTP](<https://devfeed.tech/topics/http.md>), [Web](<https://devfeed.tech/topics/web.md>), [Code](<https://devfeed.tech/topics/code.md>), [client](<https://devfeed.tech/topics/client.md>)

Tags: [building](<https://devfeed.tech/tags/building.md>), [code](<https://devfeed.tech/tags/code.md>), [console](<https://devfeed.tech/tags/console.md>), [http](<https://devfeed.tech/tags/http.md>), [port](<https://devfeed.tech/tags/port.md>), [ruby](<https://devfeed.tech/tags/ruby.md>), [scripting](<https://devfeed.tech/tags/scripting.md>), [server](<https://devfeed.tech/tags/server.md>), [web](<https://devfeed.tech/tags/web.md>)

## AI overview

A tutorial showing how to build a very small web server in Ruby 2.0+ using the built-in Socket library. It covers accepting client connections, returning a simple response, keeping the server running in a loop, and adding HTTP header information.

## Source excerpt

The Ruby language can be utilized for a variety of different purposes. The most popular of these is as a web scripting language. The entry point to these applications, the web server, is not something that is usually built from the ground up. Most applications have the lines gem 'puma' or gem 'unicorn' in their Gemfile. Those are high quality web servers that were built and are maintained by very talented people, but how do they work? The aforementioned web servers are just Ruby gems written by Rubyists. While they are awesome gems, they are not magic. A basic web server can be written to satisfy some very simple requests. GET Basic The job of a web server is to take an incoming request, decide what it means and respond accordingly. Essentially, we want to read some text, process it, and send some other text as response. To create a very simple server, the built in Socket library will be utilized. The goal of this tiny server will be to listen to localhost requests on port 8080. require 'socket' server = TCPServer.new(8080) loop do request = server.accept request.puts 'hello world' request.close end And there it is! A ridiculously tiny web server written in Ruby. Dissecting this code, we see how simple the Socket library makes this task. server = TCPServer.new(8080) This line makes a new TCPServer instance that will accept requests to port 8080 and defaults to localhost as the host. The important method on the TCPServer instance is accept. loop do Since this server is meant to respond to any request on port 8080, an infinite loop is created so that more than one request can be fulfilled before the process exits. request = server.accept request.puts 'hello world' request.close Here is the real logic of the server. The accept method returns a new instance of TCPSocket which can be used to communicate with a client. A response string, 'hello world', is then printed to the client and the connection is closed. Saving this code to example_server.rb, the server can be star