Since the ASP.NET Development server (VS2012) doesn't let us access URL over LAN (!!) and I don't have rights to configure IIS, I am trying to use WEBrick to launch a static website, to get LAN access. I am running Ubuntu with vagrant on Windows.
In the root of my static website, I have created a file app.rb
with the following content:
#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)
When I run the server; ruby app.rb
, and browse to http://localhost:8080
, it gives me this error:
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'
Is there a way to use WEBrick or Thin to run static (HTML / JS) websites?
I followed Stuart's suggestion and changed the code to:
return [200, {"Content-Type" => "text/html"}, ["index.html"]]
Now, when I am browsing to URL, it renders "index.html" as string instead rendering the file content.
There is a simple method of serving a folder over HTTP with WEBrick.
And as for Rack, it by itself doesn't deal with serving files. You have to read the file you want to serve over HTTP and give Rack the contents of the file and for that you could try this quick-and-dirty solution:
def call(env)
contents = File.open("index.html") { |f| f.read }
return [200, {"Content-Type" => "text/html"}, [contents]]
end