HBCORSMiddleware

public struct HBCORSMiddleware : HBMiddleware

Middleware implementing Cross-Origin Resource Sharing (CORS) headers.

If request has “origin” header then generate CORS headers. If method is OPTIONS then return an empty body with all the standard CORS headers otherwise send request onto the next handler and when you receive the response add a “access-control-allow-origin” header

  • Defines what origins are allowed

    See more

    Declaration

    Swift

    public enum AllowOrigin
  • Initialize CORS middleware

    Declaration

    Swift

    public init(
        allowOrigin: AllowOrigin = .originBased,
        allowHeaders: [String] = ["accept", "authorization", "content-type", "origin"],
        allowMethods: [HTTPMethod] = [.GET, .POST, .HEAD, .OPTIONS],
        allowCredentials: Bool = false,
        exposedHeaders: [String]? = nil,
        maxAge: TimeAmount? = nil
    )

    Parameters

    allowOrigin

    allow origin enum

    allowHeaders

    array of headers that are allowed

    allowMethods

    array of methods that are allowed

    allowCredentials

    are credentials alloed

    exposedHeaders

    array of headers that can be exposed back to the browser

    maxAge

    how long the results of a pre-flight request can be cached

  • apply CORS middleware

    Declaration

    Swift

    public func apply(to request: HBRequest, next: HBResponder) -> EventLoopFuture<HBResponse>