2

I start a webrick server like this:

dell@dev:/var/www/ruby$ ruby -run  -httpd. -p 5000

and have this code in abc.rb:

require 'webrick'

root   = File.path '/tmp/public_html'
server = WEBrick::HTTPServer.new :Port => 5000, :DocumentRoot => root

trap 'INT' do server.shutdown end
server.start

ary = {  "0"=>"fred", "1"=>10, "2"=>3.14, "3"=>"This is a string", "4"=>"last element", }
ary.each do |key, value|
   puts  "#{key} #{value}"
end

When I run this code it shows me the same code on browser

http://localhost:5000/abc.rb

How can I view the output this code, I have already asked this question and did not get any correct answer :(

Is it the right code? I want to know this, where this code place

require 'webrick'

root   = File.path '/tmp/public_html'
server = WEBrick::HTTPServer.new :Port => 5000, :DocumentRoot => root

trap 'INT' do server.shutdown end
server.start

if any one give me step by step ans to run this code i am very thankful.. I don't understand the ans :( how to do this

Harman
  • 1,703
  • 5
  • 22
  • 40

1 Answers1

0

From the documentation:

The easiest way to have a server perform custom operations is through WEBrick::HTTPServer#mount_proc. The block given will be called with a WEBrick::HTTPRequest with request info and a WEBrick::HTTPResponse which must be filled in appropriately:

server.mount_proc '/' do |req, res|
  res.body = 'Hello, world!'
end

Remember that server.mount_proc must server.start.

So:

require 'webrick'

root   = File.path '/tmp/public_html'
server = WEBrick::HTTPServer.new :Port => 5000, :DocumentRoot => root

server.mount_proc '/abc.rb' do |req, res|
  ary = {  "0"=>"fred", "1"=>10, "2"=>3.14, "3"=>"This is a string", "4"=>"last element" }
  res.body = ary.map do |key, value|
     "#{key} #{value}"
  end.join("\n")
end

trap 'INT' do server.shutdown end
server.start

Also, I believe the correct way to start your WebBrick is by running:

ruby abc.rb
Uri Agassi
  • 36,848
  • 14
  • 76
  • 93
  • :( No i have change my file code now as it you "require 'webrick' root = File.path '/tmp/public_html' server = WEBrick::HTTPServer.new :Port => 5000, :DocumentRoot => root server.mount_proc '/abc.rb' do |req, res| ary = { "0"=>"fred", "1"=>10, "2"=>3.14, "3"=>"This is a string", "4"=>"last element" } res.body = ary.map do |key, value| "#{key} #{value}" end.join("\n") end trap 'INT' do server.shutdown end server.start" it's not working – Harman Mar 20 '14 at 08:40
  • Please provide me step by step if you can – Harman Mar 20 '14 at 08:41
  • Did you run it as I suggested (`ruby abc.rb`)? – Uri Agassi Mar 20 '14 at 08:42
  • I have not knowledge about ubuntu I am new.. I want to run my code on server what will i do – Harman Mar 20 '14 at 08:43
  • I have just install ruby on ubuntu now, I have run code on browser "http://localhost:5000/abc.rb" but it show me code file on browser :( – Harman Mar 20 '14 at 08:44
  • i have just write down this code on abc.rb file ------------------------------- require 'webrick' root = File.path '/tmp/public_html' server = WEBrick::HTTPServer.new :Port => 5000, :DocumentRoot => root server.mount_proc '/abc.rb' do |req, res| ary = { "0"=>"fred", "1"=>10, "2"=>3.14, "3"=>"This is a string", "4"=>"last element" } res.body = ary.map do |key, value| "#{key} #{value}" end.join("\n") end trap 'INT' do server.shutdown end server.start – Harman Mar 20 '14 at 08:45
  • 2
    I suggest that you should take the time to _read_ and _understand_ the answers that people already gave you(http://stackoverflow.com/a/22438093/1120015). If you feel you don't have enough knowledge, _stop_ and _learn_ the domain you are working one. People have invested time in answering you, you should invest at least as much to take the most out of them. – Uri Agassi Mar 20 '14 at 09:07
  • this works if you save the code above in eg server.rb and start this with "ruby server.rb" in the browser then type "http://localhost:5000/abc.rb" in the adressbar However i advise to take a look at sinatra where this can be done a lot easier and with more possibilities – peter Mar 20 '14 at 12:31