The classic command for running a simple web server from a shell is:
python -m SimpleHTTPServer
This command has the advantage that it is built into Python and that Python is built into Mac OS X. However, this command always serves the current working directory (the directory that you are currently in), there is no way to provide a directory as an argument. That’s a problem if the directory you want to serve is constantly being deleted and recreated. You can’t specify the port where the files are served, either, it is always 8000.
Node.js doesn’t have a similar built-in mechanism, but there is an npm package that you can install:
npm install -g http-server
A basic way of using the shell command that this package comes with is:
http-server ["path»] [-p "port»]
Afterwards, files are served at
- If you omit the path,
./publicis used, if it exists, and
- If you omit the port, 8080 is used.
Why would you want to do this? Many styles of sites and web apps don’t run properly “over” the
file: protocol (accessed directly via the local file system). Therefore, you often need to serve them from
localhost during development.