[英]how to excute code on webrick server
I start a webrick
server like this: 我像这样启动一个
webrick
服务器:
dell@dev:/var/www/ruby$ ruby -run -httpd. -p 5000
and have this code in abc.rb
: 并在
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 如果有人给我一步一步的ans来运行这段代码,我非常感谢..我不明白ans :(如何做到这一点
From the documentation : 从文档中 :
The easiest way to have a server perform custom operations is through
WEBrick::HTTPServer#mount_proc
.让服务器执行自定义操作的最简单方法是通过
WEBrick::HTTPServer#mount_proc
。 The block given will be called with aWEBrick::HTTPRequest
with request info and aWEBrick::HTTPResponse
which must be filled in appropriately:给定的块将通过带有请求信息的
WEBrick::HTTPRequest
和必须适当填写的WEBrick::HTTPResponse
来调用:server.mount_proc '/' do |req, res| res.body = 'Hello, world!' end
Remember that
server.mount_proc
mustserver.start
.请记住,
server.mount_proc
必须为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: 另外,我相信启动
WebBrick
的正确方法是运行:
ruby abc.rb
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.