Back to Kitura

StaticFileServer

docs/docsets/Kitura.docset/Contents/Resources/Documents/Classes/StaticFileServer.html

3.0.13.1 KB
Original Source

StaticFileServer

open class StaticFileServer : RouterMiddleware

A router middleware that serves static files from a given path. By default, it will serve files from the “/public” directory.

Usage Example:

The example below creates and registers a StaticFileServer on the “/example” route. When the router is running, A user can make a request that matches the “/example” path (e.g. localhost:8080/example/hello.html). The static file server would look inside its “/files” folder for a file with the same name as the path following “/example” (e.g. “hello.html”). If a file is found it is sent as a response to that request, otherwise the next handler is called.

let router = Router()
router.all("/example", middleware: StaticFileServer(path: "./files"))

Configuration Options

`

                CacheOptions
                `

Cache configuration options for StaticFileServer.

See more

Declaration

Swift

public struct CacheOptions

`

                Options
                `

Configuration options for StaticFileServer.

See more

Declaration

Swift

public struct Options

`

                absoluteRootPath
                `

The absolute (fully qualified) root serving path for this StaticFileServer, for example: /Users/Dave/MyKituraProj/./public

Declaration

Swift

public let absoluteRootPath: String

Initializer

`

                init(path:options:customResponseHeadersSetter:)
                `

Initializes a StaticFileServer instance.

Declaration

Swift

public init(path: String = "./public", options: Options = Options(),
             customResponseHeadersSetter: ResponseHeadersSetter? = nil)

Parameters

| path |

a root directory for file serving.

| | options |

configuration options for StaticFileServer.

| | customResponseHeadersSetter |

an object of a class that implements ResponseHeadersSetter protocol providing a custom method to set the headers of the response.

|

Serve file

`

                handle(request:response:next:)
                `

Handle the request - serve static file.

Declaration

Swift

open func handle(request: RouterRequest, response: RouterResponse, next: @escaping () -> Void)

Parameters

| request |

The RouterRequest object used to work with the incoming HTTP request.

| | response |

The RouterResponse object used to respond to the HTTP request.

| | next |

The closure called to invoke the next handler or middleware associated with the request.

|