2013-05-07 28 views
2

由於ASP.NET開發服務器(VS2012)不允許我們通過局域網訪問URL(!!),並且我無權配置IIS,所以我我試圖使用WEBrick啓動一個靜態網站,以獲得局域網訪問。我在Windows上運行Ubuntu。Webrick或Thin運行(非紅寶石)靜態網站

在我的靜態網站的根,我創建了一個文件app.rb,內容如下:

#app.rb 

require 'rubygems' 
require 'rack' 

class App 
    def call(env) 
    return [200, {"Content-Type" => "text/html"}, "index.html"] 
    end 
end 

Rack::Handler::WEBrick.run(App.new, :Port => 8080) 

當我運行的服務器; ruby app.rb,並瀏覽到http://localhost:8080,它給了我這個錯誤:

ERROR NoMethodError: undefined method `each' for "index.html":String 
    /usr/local/rvm/gems/ruby-1.9.3-p392/gems/rack-1.4.5/lib/rack/handler/webrick.rb:71:in `service' 
    /usr/local/rvm/rubies/ruby-1.9.3-p392/lib/ruby/1.9.1/webrick/httpserver.rb:138:in `service' 
    /usr/local/rvm/rubies/ruby-1.9.3-p392/lib/ruby/1.9.1/webrick/httpserver.rb:94:in `run' 
    /usr/local/rvm/rubies/ruby-1.9.3-p392/lib/ruby/1.9.1/webrick/server.rb:191:in `block in start_thread' 

有沒有辦法使用的WEBrick的方式或薄運行靜態(HTML/JS)的網站?

編輯

我跟斯圖爾特的建議,改變了代碼:

return [200, {"Content-Type" => "text/html"}, ["index.html"]] 

現在,當我瀏覽到URL,它呈現「index.html的」爲字符串,而不是呈現文件內容。

回答

2

還有就是serving a folder over HTTP with WEBrick的簡單方法。

至於Rack,它本身並不處理提供的文件。你必須閱讀你想通過HTTP提供的文件,並給予文件的內容,併爲此,你可以嘗試這個快速和骯髒的解決方案:

def call(env) 
    contents = File.open("index.html") { |f| f.read } 
    return [200, {"Content-Type" => "text/html"}, [contents]] 
end 
+0

出於同樣的原因@Annie最初的命中,我在我的回答中解釋過,你需要將'contents'包裝在一個數組中(例如'[contents]')。當我逐字回答你的回答時,我得到了:'NoMethodError:undefined method \'each'for#' – 2013-05-07 17:48:51

+0

編輯,感謝修正@StuartM! – FilipK 2013-05-08 06:29:24

+0

謝謝,我複製粘貼在app.rb中的代碼塊,並運行它的工作服務器!對於任何遵循相同路徑的人,如果你需要設置固定端口將此行改爲'port = 12000 +(dir.hash%1000)'到'port = your_port_number',否則它會在12XXX端口上啓動服務器可以在代碼中看到)。 – Annie 2013-05-08 06:39:55

1

您的回覆身體必須被包裹在一個數組:

return [200, {"Content-Type" => "text/html"}, ["Hello, World!"]] 

注意[]周邊"Hello, World!"。對於爲什麼需要數組包裝的說明,請參見:

Why is rack response body an array not a string?

而且特別是Python WSGI specification的這一部分:

For large files, however, or for specialized uses of HTTP streaming (such as multipart "server push"), an application may need to provide output in smaller blocks (e.g. to avoid loading a large file into memory). It's also sometimes the case that part of a response may be time-consuming to produce, but it would be useful to send ahead the portion of the response that precedes it.

In these cases, applications will usually return an iterator (often a generator-iterator) that produces the output in a block-by-block fashion. These blocks may be broken to coincide with mulitpart boundaries (for "server push"), or just before time-consuming tasks (such as reading another block of an on-disk file).

該規格指的是Python,但在Ruby架模擬應用程序正在將HTTP響應正文封裝在Array(或任何可響應each方法的Enumerable)中。

編輯:爲了解決原來的問題的編輯,關於如何具體投放index.html文件,你可以做到這一點通過在機架響應讀取文件和輸出它的內容:

return [200, {"Content-Type" => "text/html"}, [File.read("index.html")]] 

編輯2:雖然上述僅適用於顯示index.html,但如果您想要提供靜態資源(JS,CSS等)的整個目錄,則可以嘗試使用Rack::Static中間件。看到它的文檔的細節,但我認爲你正在試圖做的可以用這個來完成的:

require 'rack/static' 
use Rack::Static, :urls => {"/" => 'index.html'} 
+0

感謝您的建議。檢查我編輯的問題。我如何渲染HTML文件的內容? – Annie 2013-05-07 10:16:45

+0

我上面編輯了我的答案,專門解決提供該文件的問題 – 2013-05-07 17:43:37

+0

非常感謝,它的工作!顯示index.html的內容。但是圖像,JS和CSS文件加載了與index.HTML相同的內容*。任何想法,如何提供所需的資源? – Annie 2013-05-08 06:22:58