Question

What does the WEBrick instance method mount_proc do (in plain English)?

The docs say:

mount_proc(dir, proc=nil, &block) 
Mounts proc or block on dir and calls it with a WEBrick::HTTPRequest and WEBrick::HTTPResponse

but I'm not clear what mounts proc on dir actually means or does.

Was it helpful?

Solution

mount_proc allows you to specify a piece of code (a proc) that will be run when a request comes in. Here's a simple hello world example adapted from the Ruby documentation

require 'webrick'

server = WEBrick::HTTPServer.new :Port => 8000
server.mount_proc '/' do |req, res|
  res.body = 'Hello, world!'
end

trap('INT') { server.stop } # stop server with Ctrl-C
server.start

now point your browser to http://localhost:8000 and you should see

Hello, world!
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top