serve current directory from command line

RubyCommand Line

Ruby Problem Overview


could someone give me a hint, howto serve the current directory from command line with ruby? it would be great, if i can have some system wide configuration (e.g. mime-types) and simply launch it from every directory.

Ruby Solutions


Solution 1 - Ruby

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

Solution 2 - Ruby

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.

Solution 3 - Ruby

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

Solution 4 - Ruby

require 'webrick'
include WEBrick

s = HTTPServer.new(:Port => 9090,  :DocumentRoot => Dir::pwd)
trap("INT"){ s.shutdown }
s.start

Solution 5 - Ruby

Use ruby gem Serve.

To install on your system, run gem install serve.

To serve a directory, simply cd to the directory and run serve.

Default port is 4000. It can also serve things like ERB, HAML, Slim and SASS.

Solution 6 - Ruby

Web Server in 1 line

This may or may not be quite what you want but it's so cool that I just had to share it.

I've used this in the past to serve the file system. Perhaps you could modify it or just accept that it serves everything.

ruby -rsocket -e 's=TCPServer.new(5**5);loop{_=s.accept;_<<"HTTP/1.0 200 OK\r\n\r\n#{File.read(_.gets.split[1])rescue nil}";_.close}'

I found it here

Chris

Solution 7 - Ruby

You can use the sinatra gem, though it doesn't do any directory listing for you, it serves files:

require 'sinatra' # gem
set :public_folder, '.'

then run that as a file, if in 1.8 add require 'rubygems' to the top first.

After running it then url's like

http://localhost:4567/file_name

should resolve to "./file_name" file.

http://localhost:4567 won't work however, since it doesn't "do" directory listings. See https://stackoverflow.com/a/12115019/32453 for a workaround there.

Solution 8 - Ruby

python3 -m http.server

or if you don't want to use the default port 8000

python3 -m http.server 3333

or if you want to allow connections from localhost only

python3 -m http.server --bind 127.0.0.1

See the docs.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSebastianView Question on Stackoverflow
Solution 1 - RubyDaniel Perez AlvarezView Answer on Stackoverflow
Solution 2 - RubyKARASZI IstvánView Answer on Stackoverflow
Solution 3 - RubyMichael KohlView Answer on Stackoverflow
Solution 4 - Rubyuser286355View Answer on Stackoverflow
Solution 5 - RubydaniniView Answer on Stackoverflow
Solution 6 - RubyChris McCauleyView Answer on Stackoverflow
Solution 7 - RubyrogerdpackView Answer on Stackoverflow
Solution 8 - RubyBoris VerkhovskiyView Answer on Stackoverflow