serveStaticFiles

Returns a request handler that serves files from the specified directory.

See sendFile for more information.

  1. HTTPServerRequestDelegateS serveStaticFiles(NativePath local_path, HTTPFileServerSettings settings)
  2. HTTPServerRequestDelegateS serveStaticFiles(string local_path, HTTPFileServerSettings settings)

Parameters

local_path string

Path to the folder to serve files from.

settings HTTPFileServerSettings

Optional settings object enabling customization of how the files get served.

Return Value

A request delegate is returned, which is suitable for registering in a URLRouter or for passing to listenHTTP.

Examples

import vibe.http.fileserver;
import vibe.http.router;
import vibe.http.server;

void setupServer()
{
	auto router = new URLRouter;
	// add other routes here
	router.get("*", serveStaticFiles("public/"));

	auto settings = new HTTPServerSettings;
	listenHTTP(settings, router);
}

This example serves all files in the "public" sub directory with an added prefix "static/" so that they don't interfere with other registered routes.

import vibe.http.fileserver;
import vibe.http.router;
import vibe.http.server;

void setupRoutes()
{
 	auto router = new URLRouter;
	// add other routes here

	auto fsettings = new HTTPFileServerSettings;
	fsettings.serverPathPrefix = "/static";
	router.get("/static/*", serveStaticFiles("public/", fsettings));

	auto settings = new HTTPServerSettings;
	listenHTTP(settings, router);
}

See Also

serveStaticFile, sendFile

Meta