Static Files
Live on this serverServes files from a directory or resource tree.
Serves the files of a folder, for example downloads or the assets of an application. GenHTTP sends each file
with the content type of its extension, and Defaults() adds compression and lets browsers cache the files.
Put your files into a files folder next to the program, so /README.md serves files/README.md.
Assets.From("./files") is a shorter way to serve a folder from disk.
To run it, create a console app (dotnet new console), add GenHTTP (dotnet add package GenHTTP.Full), replace Program.cs with the code below and start it with dotnet run - it answers at http://localhost:8080/.
using GenHTTP.Engine.Internal;
using GenHTTP.Modules.Files;
using GenHTTP.Modules.IO;
using GenHTTP.Modules.Practices;
// GET /README.md serves files/README.md
var app = Assets.From(ResourceTree.FromDirectory("./files"));
await Host.Create()
.Handler(app)
.Defaults()
.RunAsync();
Try it
Send the requests to the sample running on this server - change the path, headers or body as you like, or copy them as curl commands.
Fetch a file
curl -i 'https://genhttp.org/demo/static-files/README.md'Fetch another one
curl -i 'https://genhttp.org/demo/static-files/books.csv'