Run a web server in one line of Powershell
Here’s how to serve a static single page website in one line of Powershell.
while(1) { cat index.html | nc -w1 -l -p 8080 }
Where index.html is the file you want to serve, in your current working directory. Open a browser to http://localhost:8080 to see the “website”.
This requires netcat, which you can install with Scoop by running scoop install netcat, or download from Jon Craton here.
The -w1 option makes netcat close the connection after 1 second, which seems necessary for the Windows version of netcat. Unlike the Unix version, it doesn’t automatically close the connection when it reaches the end of the piped input. So unfortunately this is a very slow web server, unless someone knows how to fix this?
For kicks, here’s how to serve a “dynamic website” that reports the current time:
while(1) { "The time is $([datetime]::now)" | nc -w1 -l -p 8080 }
Try hitting refresh in your browser a few times to confirm that this is really working.
And here’s a dynamic REST API that returns the current time in JSON
while(1) { [datetime]::now | convertto-json | nc -w1 -l -p 8080 }
Inspired by Web server in one line of bash