How to run a server on localhost

Objective

Get an http server running as quickly as possible.

Instructions

Here are a few ways to get a server running quickly on localhost (ordered from fastest to slowest):

Go

Compile the following using go build and run the resulting binary:

package main

import (
    "fmt"; "log"; "net/http"
)

func main() {
    fmt.Println("Serving files in the current directory on http://localhost:8080")
    http.Handle("/", http.FileServer(http.Dir(".")))
    err := http.ListenAndServe(":8080", nil)
    if err != nil {
        log.Fatal("ListenAndServe: ", err)
    }
}

Alternatively you can run the script using go run… or you can get hack Bash and Go together and call the script as an executable by prepending the following (mutated) shebang:

//usr/bin/env go run "$0" "$@"; exit "$?"

Remember to make the script executable with:

chmod 744

Node.js

http-server

Install http-server using npm:

npm install http-server -g

Then you can run the server using

http-server

serve

Lately, this has been my go to:

npm install serve -g

Then you can run the server using

serve

Python 3.x

python3 -m http.server

Python 2.x

python2 -m SimpleHTTPServer