command-linehttpservercommand-line-toolsimplehttpserver

What is a faster alternative to Python's http.server (or SimpleHTTPServer)?


Python's http.server (or SimpleHTTPServer for Python 2) is a great way of serve the contents of the current directory from the command line:

python -m http.server

However, as far as web servers go, it's very slooooow...

It behaves as though it's single threaded, and occasionally causes timeout errors when loading JavaScript AMD modules using RequireJS. It can take five to ten seconds to load a simple page with no images.

What's a faster alternative that is just as convenient?


Solution

  • http-server for node.js is very convenient, and is a lot faster than Python's SimpleHTTPServer. This is primarily because it uses asynchronous IO for concurrent handling of requests, instead of serialising requests.

    Installation

    Install node.js if you haven't already. Then use the node package manager (npm) to install the package, using the -g option to install globally. If you're on Windows you'll need a prompt with administrator permissions, and on Linux/OSX you'll want to sudo the command:

    npm install http-server -g
    

    This will download any required dependencies and install http-server.

    Use

    Now, from any directory, you can type:

    http-server [path] [options]
    

    Path is optional, defaulting to ./public if it exists, otherwise ./.

    Options are [defaults]:

    So to serve the current directory on port 8000, type:

    http-server -p 8000