Last active
May 31, 2018 04:56
-
-
Save nathanlong/bc1d4923982ad45e9379 to your computer and use it in GitHub Desktop.
Local Servers from the Command Line
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Drop these functions into your ~/.bashrc or ~/.bash_profile, wherever you keep your bashy goodness. | |
# Usage: cd to directory you want to serve up, fire one of these commands, navigate to http://0.0.0.0:{port number here} | |
# Start an HTTP server from a directory, optionally specifying the port | |
function serverpy() { | |
local port="${1:-8000}" | |
open "http://localhost:${port}/" | |
# Set the default Content-Type to `text/plain` instead of `application/octet-stream` | |
# And serve everything as UTF-8 (although not technically correct, this doesn’t break anything for binary files) | |
python -c $'import SimpleHTTPServer;\nmap = SimpleHTTPServer.SimpleHTTPRequestHandler.extensions_map;\nmap[""] = "text/plain";\nfor key, value in map.items():\n\tmap[key] = value + ";charset=UTF-8";\nSimpleHTTPServer.test();' "$port" | |
} | |
# Start a PHP server from a directory, optionally specifying the port | |
# (Requires PHP 5.4.0+.) | |
function phpserver() { | |
local port="${1:-4000}" | |
local ip=$(ipconfig getifaddr en1) | |
sleep 1 && open "http://${ip}:${port}/" & | |
php -S "${ip}:${port}" | |
} | |
# Start webrick server from current directory | |
function server { | |
local port=$1 | |
: ${port:=3000} | |
open "http://localhost:${port}/" | |
ruby -rwebrick -e"s = WEBrick::HTTPServer.new(:Port => $port, :DocumentRoot => Dir.pwd, :MimeTypes => WEBrick::HTTPUtils::load_mime_types('/etc/apache2/mime.types')); trap(%q(INT)) { s.shutdown }; s.start" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
try #go https://gist.github.com/sblack4/0f083a798f6dfd40640a62162785c2cb 😃