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 (/).
    See more

    Declaration

    Swift

    public class Router
  • 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. This Application.run function would then be called in your main.swift file as a non-returning function to initilize your Kitura server.

    let router = Router()
    let port: Int = 8080
    public func run() throws{
    ...
       Kitura.addHTTPServer(onPort: port, with: router)
       Kitura.run()
    }
    
    See more

    Declaration

    Swift

    public class Kitura
  • A set of helper functions for router path matching using regular expression.

    See more

    Declaration

    Swift

    public class RouteRegex
  • 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 the RouterRequest body field with a corresponding ParsedBody enumeration.

    In order for the BodyParser to be used it must first be registered with any routes that are interested in the ParsedBody payload.

    ParsedBody enumeration:

    The mappings from the incoming Content-Type to an internal representation of the body are as follows:

       .json([String: Any])          // "application/json"
       .text(String)                 // "text/*"
       .urlEncoded([String:String])  // "application/x-www-form-urlencoded"
       .multipart([Part])            // "multipart/form-data"
       .raw(Data)                    // Any other Content-Type
    

    Each case has a corresponding convenience property, e.g. asURLEncoded: [String:String], for accessing the associated data.

    Note: If you have not declared a Content-Type header, ParsedBody will be nil.

    Usage Example:

    In this example, all routes to the BodyParser middleware are registered to the BodyParser middleware. A request with application/json, ContentType header is received. It is then parsed as JSON and the value for name is returned in the response.

    router.all("/name", middleware: BodyParser())
    router.post("/name") { request, response, next in
        guard let jsonBody = request.parsedBody?.asJSON else {
            next()
            return
        }
        let name = jsonBody["name"] as? String ?? ""
        try response.send("Hello \(name)").end()
    }
    

    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 No data in request. Codable routes do not allow the use of a BodyParser. and the route handler will not be executed).

    See more

    Declaration

    Swift

    public class BodyParser: RouterMiddleware
  • Undocumented

    See more

    Declaration

    Swift

    public class BodyParserMultiValue: BodyParser
  • 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 file foo.png, which is identified as image/png.

    let contentType = ContentType.sharedInstance
    let result = contentType.getContentType(forFileName: "foo.png")
    print(String(describing: result))
    // Prints Optional("image/png")
    
    See more

    Declaration

    Swift

    public class ContentType