serve current directory from command line

As Aaron Patterson tweeted it out today you can do:

ruby -run -e httpd . -p 5000

And you can set the bind address as well by adding -b 127.0.0.1

Works with Ruby 1.9.2 and greater.


I've never seen anything as compact as

python3 -m http.server

You can optionally add a port number to the end:

python3 -m http.server 9000

See https://docs.python.org/library/http.server.html


Simplest way possible (thanks Aaron Patterson/n0kada):

ruby -run -e httpd . -p 9090

Alternate, more complex way:

ruby -r webrick -e "s = WEBrick::HTTPServer.new(:Port => 9090, :DocumentRoot => Dir.pwd); trap('INT') { s.shutdown }; s.start"

Even the first command is hard to remember, so I just have this in my .bashrc:

function serve {
  port="${1:-3000}"
  ruby -run -e httpd . -p $port
}

It serves the current directory on port 3000 by default, but you can also specify the port:

~ $ cd tmp
~/tmp $ serve      # ~/tmp served on port 3000
~/tmp $ cd ../www
~/www $ serve 5000   # ~/www served on port 5000