Local Dev Server for Static Files
Fri, Jul 19, 2019If you are looking for a quick way to start up a local dev server for serving static files, there are many options available. I’m going to describe some of the ones I use.
Live Server - VS Code extension
… there is an extension for that!
Yep, with the sheer amount of VS Code extensions available there is usually at least one that solves your problem. Live Server is simple and does exactly what it advertises. After installation you can just click in the bottom right corner and it will fire up a server and also open a browser.
SimpleHTTPServer or http.server with python
If you have python installed on your machine there is a module, that gives you the ability to start a server from the current directory. In python 2 it’s the SimpleHTTPServer, in python 3 the http.server module.
To start up the server, on port 12345, open command line and enter
python -m http.server 12345 --bind 127.0.0.1
This is nice since I don’t need to open VS Code for it. Of course I don’t want to type that command everytime, so I have written little script that looks like this
@echo off
set /a port=%random% +10000
python -m http.server %port% --bind 127.0.0.1
and saved it to a file named http.cmd to a directory C://batch, which I have added to the path environment variable.
That gives me the option to just type http anywhere and …
No python? What about WSL?
If you don’t have or don’t want to install python, maybe you have WSL (Windows Subsystem for Linux) installed. With WSL installed you can open command line and enter
wsl python3 -m http.server 12345 --bind 127.0.0.1
For strictly C# oriented folks there is a nice code sample on github that works like SimpleHTTPServer. You just need to compile it into console app.