Skip to content

Instantly share code, notes, and snippets.

@marksowell
Last active January 8, 2025 23:22
Show Gist options
  • Save marksowell/37800be66caa54727b4a721807a30410 to your computer and use it in GitHub Desktop.
Save marksowell/37800be66caa54727b4a721807a30410 to your computer and use it in GitHub Desktop.
Simple HTTP Servers in Various Languages and Tools

Simple HTTP Servers in Various Languages and Tools

Python

python -m http.server 8000

Serves the current directory on port 8000.

Go

go run -e "package main; import ('net/http'); func main() {http.ListenAndServe(':8000', http.FileServer(http.Dir('.')))}"

Serves the current directory on port 8000.

Rust

Using the cargo install simple-http-server (install if not done yet):

simple-http-server --port 8000

Ruby

ruby -run -e httpd . -p 8000

Serves the current directory on port 8000.

PHP

php -S 0.0.0.0:8000

Serves the current directory on port 8000.

Perl

perl -MHTTP::Server::Brick -e '$s = HTTP::Server::Brick->new(port => 8000); $s->mount("/" => {path => "."}); $s->start'

Serves the current directory on port 8000.

Apache (httpd)

Apache typically requires configuration, but to launch a temporary server in the current directory, use:

httpd -f /dev/null -c "ServerRoot ." -c "Listen 8000" -c "DocumentRoot $(pwd)"

Java

Using a small embedded HTTP server:

jshell -R -e 'import com.sun.net.httpserver.*; var server = HttpServer.create(new InetSocketAddress(8000), 0); server.createContext("/", h -> {var os = h.getResponseBody(); h.sendResponseHeaders(200, 0); os.write("Hello, World!".getBytes()); os.close();}); server.start();'

Node.js (JavaScript)

Install http-server globally if not done yet:

npm install -g http-server

Then run:

http-server -p 8000

Alternatively, a one-liner directly in Node.js:

node -e "require('http').createServer((req, res) => res.end('Hello, World!')).listen(8000)"

Bash (using netcat)

while true; do echo -e "HTTP/1.1 200 OK\n\nHello, World!" | nc -l 8000; done

Not a file server, but serves a basic HTTP response.

Lua

Using the lua-http library (install if not done yet):

lua -e 'local http = require("http.server").new("0.0.0.0", 8000); http:run()'

Node.js (Using npx http-server)

npx http-server

Serves the current directory on a default port.

Deno

deno run --allow-net=:8000 https://deno.land/std/http/file_server.ts

Serves the current directory on port 8000.

Python Flask (for more flexibility)

For a more feature-rich Python HTTP server:

pip install flask
python -c "from flask import Flask, send_from_directory; app = Flask(__name__); app.route('/<path:path>')(lambda path: send_from_directory('.', path)); app.run(port=8000)"

A minimal Flask server serving static files from the current directory on port 8000.

JavaScript ES6 Modules

If you want to keep things in vanilla Node.js:

node -e "import('http').then(({createServer})=>createServer((_,res)=>res.end('Hello, World!')).listen(8000))"

Simple ES6-compatible HTTP server.

PowerShell

For Windows users, PowerShell offers an easy solution:

Start-Process -NoNewWindow powershell -ArgumentList '-command $h=New-Object Net.HttpListener;$h.Prefixes.Add("http://*:8000/");$h.Start();while($h.IsListening){$c=$h.GetContext();$r=$c.Response;$r.OutputStream.Close()}'

Starts a simple HTTP server on port 8000.

Caddy

Caddy is a powerful, secure web server with automatic HTTPS:

caddy file-server --listen :8000

Serves the current directory on port 8000 with minimal configuration.

BusyBox HTTPd

For embedded systems and lightweight environments:

busybox httpd -f -p 8000

A simple HTTP server for lightweight systems.

Haskell

stack exec runghc -- -e "import Network.Wai.Handler.Warp (run); import Network.Wai.Application.Static (staticApp, defaultFileServerSettings); run 8000 (staticApp (defaultFileServerSettings "."))"

Serves the current directory using Haskell.

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