Skip to content

Instantly share code, notes, and snippets.

@spenkk
Last active May 6, 2025 12:37
Show Gist options
  • Save spenkk/48e2597699185b4cbe26d702870e3e30 to your computer and use it in GitHub Desktop.
Save spenkk/48e2597699185b4cbe26d702870e3e30 to your computer and use it in GitHub Desktop.
$listener = New-Object System.Net.HttpListener; $listener.Prefixes.Add("http://localhost:8080/"); $listener.Start(); while ($listener.IsListening) { $context = $listener.GetContext(); $req = $context.Request; $res = $context.Response; $path = Join-Path (Get-Location) ($req.Url.LocalPath.TrimStart('/')); if (Test-Path $path -PathType Leaf) { $content = [System.IO.File]::ReadAllBytes($path); $res.ContentLength64 = $content.Length; $res.OutputStream.Write($content, 0, $content.Length) } else { $res.StatusCode = 404; $content = [System.Text.Encoding]::UTF8.GetBytes("File not found"); $res.ContentLength64 = $content.Length; $res.OutputStream.Write($content, 0, $content.Length) }; $res.Close() }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment