Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WEBrick: mount_proc

Tags:

ruby

proc

webrick

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.

like image 393
Snowcrash Avatar asked Jan 30 '26 09:01

Snowcrash


1 Answers

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!
like image 123
Patrick Oscity Avatar answered Feb 02 '26 02:02

Patrick Oscity



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!