WEBrick server

Contents
Serve a directory
Mount a proc
Serve JSON
Trap INT to stop

Serve a directory

WEBrick ships with Ruby and serves static files with two lines of code. Point DocumentRoot at a folder and start the server.

Open the shown port in a browser to browse the folder content during local development.

require "webrick" server = WEBrick::HTTPServer.new(Port: 8000, DocumentRoot: ".") server.start

Mount a proc

Dynamic paths need code, not files. Mount a path with a block that receives the request and builds the response.

The block runs on every visit, so the response can depend on query params, headers, and time of day.

require "webrick" server = WEBrick::HTTPServer.new(Port: 8000) server.mount_proc("/hello") do |req, res| res.body = "hi" end server.start

Serve JSON

Small APIs return JSON instead of HTML pages. Set the content type header and dump a Ruby structure with JSON generate.

Clients parse the body back into arrays and hashes, exactly as the JSON lesson describes.

require "webrick" require "json" server = WEBrick::HTTPServer.new(Port: 8000) server.mount_proc("/todos") do |req, res| res["Content-Type"] = "application/json" res.body = JSON.generate([{ title: "write tests" }]) end server.start

Trap INT to stop

A server started from the terminal owns that terminal until it stops. Trap the interrupt signal so Control plus C shuts it down cleanly.

Without the trap the process may need a forced kill, which can leave the port busy for a while.

require "webrick" server = WEBrick::HTTPServer.new(Port: 8000) trap("INT") { server.shutdown } server.start

curl http://localhost:8000/hello

hi

Next: Rails in one lesson

Article author: Arthur Isaev

Related articles
Ruby
Ruby course: zero to hero in 50 lessons
What is Ruby and where it runs
Install Ruby and check the version
irb: the interactive shell
Your first Ruby script
Variables
Numbers
Strings
Symbols
Truthiness: only false and nil
Conditions
Loops
Arrays
Hashes
Ranges
Methods
Blocks, procs, lambdas
String tools
Files and IO
Exceptions
Modules and mixins
Classes and objects
Inheritance
Enumerables
Duck typing and core idioms
Gems and Bundler
Testing with Minitest
Debugging
Encodings
Time and date
JSON and YAML
HTTP clients
Sockets
REST APIs
puts, print and p
Metaprogramming
Enumerators
Pattern matching
Arguments: splats and kwargs
Rake tasks
Environment and CLI
Logging
CSV
ERB templates
WEBrick server
Rails in one lesson
Capstone: todo CLI
Capstone: JSON API
Performance
Modern Ruby tour
Hero roadmap: the whole course on one page
Install Ruby on Windows 11
Install Ruby on Ubuntu
Install Ruby on Rocky Linux
Install Ruby on macOS
Install Ruby on FreeBSD
Ruby in Docker
Threads and fibers

Search this site

Channel @aofeed Chat @aofeedchat

Contacts and cooperation:
I recommend our hosting beget.ru
Write to info@urn.su, info@aredel.com if you:
1. Want to write an article for our site or translate an article into your native language.
2. Want to place thematically relevant ads on the site.
3. Ads on my site pass maximum censorship. If you see an ad block unsuitable for school-age children, shocking or misleading - please contact us by e-mail
4. Found a mistake, inaccuracy, bug, etc. on the site. ... .......
5. Articles can be shared on social media by clicking a network icon: