Executable HTTP Server Script

Filed under:

Get cross-domain errors in the console because you’re running file:// instead of a real http:// server suck.
I rarely use the file protocol anymore because it’s way simpler to just start a server from terminal, it’s baked into OS X!
Also I hate starting MAMP for anything without a database.

The only real magic it requires is a python command: python -m SimpleHTTPServer but I simplified this even more and created a noob-proof executable script, which you can just double click to start the server.

It will also check if you have an entry point (index.html for example) and only run the server if the entry point file exists. Otherwise it will send a notification (10.8+) with the error message. If the entry file is found it starts the server and opens it in the browser.

Double clicking is the only required skill here!

cd "$(dirname "$0")" #Get current directory

ENTRY=index.html #Set the entry point for the server

if [ -f "$ENTRY" ]; then #Check if the entry exists then go on
  sleep 1 && open "http://localhost:8000/"; #Delay a second then open localhost
  python -m SimpleHTTPServer; #Start the HTTP Server
else #Send an alert
  osascript -e 'display notification "No entry point found, quitting." with title "Missing index.html"'

Getting errors?
Try cd’ing to the directory where you placed the Server Script and use chmod +x HTTPServer.command to make it executable again.

More about the python command it possible options like port specifying? Read it here