Classes
The following classes are available globally.
-
Router
provides the external interface for routing requests to the appropriate code to handle them. This includes:- Routing requests to closures of type
RouterHandler
- Routing requests to the handle function of classes that implement the
RouterMiddleware
protocol. - Routing requests to a
TemplateEngine
to generate the appropriate output. - Serving the landing page when someone makes an HTTP request with a path of slash (/).
Declaration
Swift
public class Router
- Routing requests to closures of type
-
A set of helper functions to make it easier to create, start, and stop Kitura based servers.
Usage Example:
In this example, a function called run is created, inside a server
Application.swift
file. This will create a Kitura server on the specified port, using the given router and run this created server. ThisApplication.run
function would then be called in yourmain.swift
file as a non-returning function to initilize your Kitura server.
See morelet router = Router() let port: Int = 8080 public func run() throws{ ... Kitura.addHTTPServer(onPort: port, with: router) Kitura.run() }
Declaration
Swift
public class Kitura
-
A set of helper functions for router path matching using regular expression.
See moreDeclaration
Swift
public class RouteRegex
-
Create an on the fly
See moreRouterMiddleware
from aRouterHandler
closure.Declaration
Swift
public class RouterMiddlewareGenerator: RouterMiddleware
-
Router request.
See moreDeclaration
Swift
public class RouterRequest
-
Router response that the server sends as a reply to
See moreRouterRequest
.Declaration
Swift
public class RouterResponse
-
The
BodyParser
parses the body of the request prior to sending it to the handler. It reads the Content-Type of the message header and populates theRouterRequest
body field with aParsedBody
enumeration (e.g. json, raw, text, urlEncoded). In order for the BodyParser to be used it must first be registered with any routes that are interested in the ParsedBody payload.Usage Example:
In this example, all routes to the BodyParser middleware are registered to the
BodyParser
middleware.router.all("/*", middleware: BodyParser())
Note: When using Codable Routing in Kitura 2.x the BodyParser should not be registered to any codable routes (doing so will log the following error
See moreNo data in request. Codable routes do not allow the use of a BodyParser.
and the route handler will not be executed).Declaration
Swift
public class BodyParser: RouterMiddleware
-
The
ContentType
class provides functions to determine the MIME content type for a given file extension. The user can pass in a complete file name e.g.foo.png
or just the file extension e.g.png
, or they can pass in both a MIME content type and a file extension and query whether they match.Usage Example:
In this example, a
ContentType
instance is initialised called contentType. This instance is then used to obtain the MIME content type of the filefoo.png
, which is identified asimage/png
.
See morelet contentType = ContentType.sharedInstance let result = contentType.getContentType(forFileName: "foo.png") print(String(describing: result)) // Prints Optional("image/png")
Declaration
Swift
public class ContentType
-
A router middleware that serves static files from a given path.
See moreDeclaration
Swift
open class StaticFileServer: RouterMiddleware