As a DevOps engineer or system administrator, you might want to quickly spin up a HTTP server that allows to access files from other machines or to test functionality of programs and apps. Doing so would traditionally require you to install and configure a web server such as nginx or Apache, or to run a Docker image with volumes to make your target files accessible.
With .NET file-based apps, we can create script files that are easily capable of using any library hosted on nuget. In this article, we will demonstrate how to use the GenHTTP webserver to serve a local directory via HTTP.
Pre-Requisites
To run the script, you will need the .NET SDK (version 10 or higher) to be installed on your device (see this manual for Linux).
Creating the Script
In a local folder, create a new file named file-server.csx and paste the following content:
#!/usr/local/share/dotnet/dotnet run
#:package GenHTTP.Core@10.2.0
#:package GenHTTP.Modules.DirectoryBrowsing@10.2.0
using GenHTTP.Engine.Internal;
using GenHTTP.Modules.IO;
using GenHTTP.Modules.Practices;
using GenHTTP.Modules.DirectoryBrowsing;
var directory = @"./"; // adjust as needed
var tree = ResourceTree.FromDirectory(directory);
var app = Listing.From(tree);
await Host.Create()
.Handler(app)
.Defaults()
.RunAsync();
This script will instruct the server to host a simple HTML-based file browser application that serves the current directory. Adjust the path to your target directory as needed.
Running the Script
On Windows, the script can be started in a terminal window by running
dotnet run file-server.csx
On Linux, the shebang allows us to directly execute the file. After marking the file as executable via
chmod +x file-server.csx
we can directly start the server via
./file-server.csx
When started, the server will block until CTRL + C is pressed. You will now be able to list and download the target files via http://localhost:8080 in your browser or from other apps:
Cover picture by Fabien Barral

Top comments (0)