Classes

The following classes are available globally.

  • The Router class provides the external interface for the routing of requests to the appropriate code for handling. This includes:

    • Routing requests to closures with the signature of RouterHandler
    • Routing requests to the handle function of classes that implement the RouterMiddleware protocol.
    • Routing the request to a template engine 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 ParsedBody 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 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
  • 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