Skip to content

Instantly share code, notes, and snippets.

@apchamberlain
Last active July 16, 2018 20:47
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save apchamberlain/99d7397a755373fc678d to your computer and use it in GitHub Desktop.
Save apchamberlain/99d7397a755373fc678d to your computer and use it in GitHub Desktop.
One-line web servers

Instant Webservers! Just add electrons!

All these run on localhost:8000 unless otherwise noted and serve the index.html file in the current directory in an insecure session, i.e, open http://127.0.0.1:8000 in any browser. All except Python's SimpleHTTPServer allow multiple simultaneous client connections. The Node http-server module is the most robust although none of these is suitable for production. The true one-liners (all but Node) should all run without any dependencies on a standard OS X or Linux install.

Python 2

    python -m SimpleHTTPServer

or

    python2 -m SimpleHTTPServer

Perl, pure socket-level implementation (!)

    perl -MIO::All -e 'io(":8000")->fork->accept->(sub { $_[0] < io(-x $1 ? "./$1 |" : $1) if /^GET \/(.*) / })'

Ruby

    ruby -rwebrick -e'WEBrick::HTTPServer.new(:Port => 8000, :DocumentRoot => Dir.pwd).start'

JavaScript

Not quite a one-liner, and it does put stuff in the local dir (the usual npm baggage), but extremely low overhead and high performance as you would expect with Node.

    npm install http-server
    http-server

N.B. Node also has the simplest possible REST server for testing in http://stub.by/ if that's what you're really looking for.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment