CORS
CORS middleware implements CORS specification. CORS gives web servers cross-domain access controls, which enable secure cross-domain data transfers.
Installation
- go get -u github.com/gofiber/cors
Signature
- cors.New(config ...Config) func(*fiber.Ctx)
Config
Property | Type | Description | Default |
---|---|---|---|
Filter | func(Ctx) bool | Defines a function to skip middleware | nil |
AllowOrigins | []string | AllowOrigin defines a list of origins that may access the resource. | []string{““} |
AllowMethods | []string | AllowMethods defines a list methods allowed when accessing the resource. This is used in response to a preflight request. | []string{“GET”, “POST”, “HEAD”, “PUT”, “DELETE”, “PATCH”} |
AllowCredentials | string | AllowCredentials indicates whether or not the response to the request can be exposed when the credentials flag is true. When used as part of a response to a preflight request, this indicates whether or not the actual request can be made using credentials. | nil |
ExposeHeaders | []string | ExposeHeaders defines a whitelist headers that clients are allowed to access. | nil |
MaxAge | int | MaxAge indicates how long (in seconds) the results of a preflight request can be cached. | 0 |
- package main
- import (
- "github.com/gofiber/fiber"
- "github.com/gofiber/cors"
- )
- func main() {
- app := fiber.New()
- app.Use(cors.New())
- app.Get("/", func(c *fiber.Ctx) {
- c.Send("Welcome!")
- })
- app.Listen(3000)
- // curl -H "Origin: http://example.com" --verbose http://localhost:3000
- }