Last Updated: February 25, 2016
·
1.545K
· koffeinfrei

serve static html (e.g. prototype) with rack

If you have e.g. a static html prototype but want to run it properly in a server (i.e. you want server relative links etc. to work properly) you can quickly serve up your static pages inside the current directory with rack, without the need to create an apache virtual host or something or copy your pages somewhere else. The only thing you need to do is create a rack config-file:

# config.ru
run Rack::Directory.new(Dir.pwd)

Then run rackup -p 4567 to serve your static html under http://localhost:4567

EDIT:
Even simpler without the need for a config.ru

ruby -run -e httpd . -p5000

4 Responses
Add your response

Looks cool. Performance-wise, how would this compare to something like storing static files on S3?

over 1 year ago ·

@adelevie: I can't give you any useful information about performance as a) i don't have any benchmarks and b) I don't really care because this setup is meant for development primarily.
I assume though that the performance should not be a problem, as

a) It's rack, and rack is fast
b) you can configure which rack server to use
c) it only serves static files, so there's hardly no overhead
d) the bottleneck will be HTTP and the network

That being said, I don't think that you'll have any noticeable performance differences between serving from s3 and serving from rack.

over 1 year ago ·

python2 -m SimpleHTTPServer <port>
or
python3 -m http.server <port>
would also work

over 1 year ago ·

I'm a Rubyist, but I needed to do some development on localhost, and was getting ready to fire up an Apache server when I found this. Huzzah!

over 1 year ago ·