One Line Ruby Webserver

This weekend I’ve been working on an iPhone app which is entirely HTML, CSS, and Javascript. I’ve been missing script/server as an easy way to spawn a web server for testing on an iPhone.

There’s a Python one-liner I usually use for this sort of thing, but I don’t otherwise use Python and figured it would be better to do it in Ruby. I mentioned this on Twitter and Craig Webster delivered the goods.

There’s a slightly more advanced – but also clunkier – version in the comments which I’ve stuck in a shell alias to make life simple. Put this in your .bash_profile and you can use it too!

alias serve="ruby -rwebrick -e\"s = WEBrick::HTTPServer.new(:Port => 3000, :DocumentRoot => Dir.pwd); trap('INT') { s.shutdown }; s.start\""

Now I can just type ‘serve’ and navigate to http://109.224.145.50:3000/ to check out how the files being served from my laptop look on my iPhone.

Good times!