December 17, 2013

Run a simple HTTP server with one command line

Recently someone told me this great and simple trick on how to run an HTTP web server with just one command line. This is useful for a million things, but I use it for prototyping and avoid same-origin policy in Chrome. Sure, I know there’s a way to disable it, but you may not want to do that.

XMLHttpRequest cannot load file:///C:/Projects/Test/app/views/shell.html. Cross origin requests are only supported for HTTP.

This is all it takes:

$ cd /projects/TestProject
$ python -m SimpleHTTPServer 1234

Then you can access your files at localhost:1234.

I think this provides a much real environment while you build your HTML/JS. That way, you can have a more accurate preview on how your UI is going to feel when served through the Internet by a real server.

Note: This can also be achieved using PHP and probably a gazillion other programming languages.