github.com/gofiber/fiber/v2@v2.47.0/docs/api/fiber.md (about)

     1  ---
     2  id: fiber
     3  title: 📦 Fiber
     4  description: Fiber represents the fiber package where you start to create an instance.
     5  sidebar_position: 1
     6  ---
     7  
     8  ## New
     9  
    10  This method creates a new **App** named instance. You can pass optional [config ](#config)when creating a new instance.
    11  
    12  ```go title="Signature"
    13  func New(config ...Config) *App
    14  ```
    15  
    16  ```go title="Example"
    17  // Default config
    18  app := fiber.New()
    19  
    20  // ...
    21  ```
    22  
    23  ## Config
    24  
    25  You can pass an optional Config when creating a new Fiber instance.
    26  
    27  ```go title="Example"
    28  // Custom config
    29  app := fiber.New(fiber.Config{
    30      Prefork:       true,
    31      CaseSensitive: true,
    32      StrictRouting: true,
    33      ServerHeader:  "Fiber",
    34      AppName: "Test App v1.0.1"
    35  })
    36  
    37  // ...
    38  ```
    39  
    40  **Config fields**
    41  
    42  | Property                     | Type                  | Description                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    | Default               |
    43  | ---------------------------- | --------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | --------------------- |
    44  | AppName                      | `string`              | This allows to setup app name for the app                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      | `""`                  |
    45  | BodyLimit                    | `int`                 | Sets the maximum allowed size for a request body, if the size exceeds the configured limit, it sends `413 - Request Entity Too Large` response.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                | `4 * 1024 * 1024`     |
    46  | CaseSensitive                | `bool`                | When enabled, `/Foo` and `/foo` are different routes. When disabled, `/Foo`and `/foo` are treated the same.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    | `false`               |
    47  | ColorScheme                  | [`Colors`](https://github.com/gofiber/fiber/blob/master/color.go) | You can define custom color scheme. They'll be used for startup message, route list and some middlewares.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      | [`DefaultColors`](https://github.com/gofiber/fiber/blob/master/color.go) |
    48  | CompressedFileSuffix         | `string`              | Adds a suffix to the original file name and tries saving the resulting compressed file under the new file name.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                | `".fiber.gz"`         |
    49  | Concurrency                  | `int`                 | Maximum number of concurrent connections.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      | `256 * 1024`          |
    50  | DisableDefaultContentType    | `bool`                | When set to true, causes the default Content-Type header to be excluded from the Response.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     | `false`               |
    51  | DisableDefaultDate           | `bool`                | When set to true causes the default date header to be excluded from the response.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              | `false`               |
    52  | DisableHeaderNormalizing     | `bool`                | By default all header names are normalized: conteNT-tYPE -> Content-Type                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    | `false`               |
    53  | DisableKeepalive             | `bool`                | Disable keep-alive connections, the server will close incoming connections after sending the first response to the client                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      | `false`               |
    54  | DisablePreParseMultipartForm | `bool`                | Will not pre parse Multipart Form data if set to true. This option is useful for servers that desire to treat multipart form data as a binary blob, or choose when to parse the data.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          | `false`               |
    55  | DisableStartupMessage        | `bool`                | When set to true, it will not print out debug information                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      | `false`               |
    56  | ETag                         | `bool`                | Enable or disable ETag header generation, since both weak and strong etags are generated using the same hashing method \(CRC-32\). Weak ETags are the default when enabled.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    | `false`               |
    57  | EnableIPValidation           | `bool`                | If set to true, `c.IP()` and `c.IPs()` will validate IP addresses before returning them. Also, `c.IP()` will return only the first valid IP rather than just the raw header value that may be a comma seperated string.<br /><br />**WARNING:** There is a small performance cost to doing this validation. Keep disabled if speed is your only concern and your application is behind a trusted proxy that already validates this header.                                                                                                                                                                                                                                                                                                                                                                                     | `false`               |
    58  | EnablePrintRoutes            | `bool`                | EnablePrintRoutes enables print all routes with their method, path, name and handler..                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         | `false`               |
    59  | EnableTrustedProxyCheck      | `bool`                | When set to true, fiber will check whether proxy is trusted, using TrustedProxies list. <br /><br />By default  `c.Protocol()` will get value from X-Forwarded-Proto, X-Forwarded-Protocol, X-Forwarded-Ssl or X-Url-Scheme header, `c.IP()` will get value from `ProxyHeader` header, `c.Hostname()` will get value from X-Forwarded-Host header. <br /> If `EnableTrustedProxyCheck` is true, and `RemoteIP` is in the list of `TrustedProxies` `c.Protocol()`, `c.IP()`, and `c.Hostname()` will have the same behaviour when `EnableTrustedProxyCheck` disabled, if `RemoteIP` isn't in the list, `c.Protocol()` will return https in case when tls connection is handled by the app, or http otherwise, `c.IP()` will return RemoteIP() from fasthttp context, `c.Hostname()` will return `fasthttp.Request.URI().Host()` | `false`               |
    60  | ErrorHandler                 | `ErrorHandler`        | ErrorHandler is executed when an error is returned from fiber.Handler. Mounted fiber error handlers are retained by the top-level app and applied on prefix associated requests.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               | `DefaultErrorHandler` |
    61  | GETOnly                      | `bool`                | Rejects all non-GET requests if set to true. This option is useful as anti-DoS protection for servers accepting only GET requests. The request size is limited by ReadBufferSize if GETOnly is set.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            | `false`               |
    62  | IdleTimeout                  | `time.Duration`       | The maximum amount of time to wait for the next request when keep-alive is enabled. If IdleTimeout is zero, the value of ReadTimeout is used.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  | `nil`                 |
    63  | Immutable                    | `bool`                | When enabled, all values returned by context methods are immutable. By default, they are valid until you return from the handler; see issue [\#185](https://github.com/gofiber/fiber/issues/185).                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              | `false`               |
    64  | JSONDecoder                  | `utils.JSONUnmarshal` | Allowing for flexibility in using another json library for decoding.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           | `json.Unmarshal`      |
    65  | JSONEncoder                  | `utils.JSONMarshal`   | Allowing for flexibility in using another json library for encoding.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           | `json.Marshal`        |
    66  | Network                      | `string`              | Known networks are "tcp", "tcp4" (IPv4-only), "tcp6" (IPv6-only)<br /><br />**WARNING:** When prefork is set to true, only "tcp4" and "tcp6" can be chosen.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        | `NetworkTCP4`         |
    67  | PassLocalsToViews            | `bool`                | PassLocalsToViews Enables passing of the locals set on a fiber.Ctx to the template engine. See our **Template Middleware** for supported engines.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              | `false`               |
    68  | Prefork                      | `bool`                | Enables use of the[`SO_REUSEPORT`](https://lwn.net/Articles/542629/)socket option. This will spawn multiple Go processes listening on the same port. learn more about [socket sharding](https://www.nginx.com/blog/socket-sharding-nginx-release-1-9-1/). **NOTE: if enabled, the application will need to be ran through a shell because prefork mode sets environment variables. If you're using Docker, make sure the app is ran with `CMD ./app` or `CMD ["sh", "-c", "/app"]`. For more info, see** [**this**](https://github.com/gofiber/fiber/issues/1021#issuecomment-730537971) **issue comment.**                                                                                                                                                                                                                    | `false`               |
    69  | ProxyHeader                  | `string`              | This will enable `c.IP()` to return the value of the given header key. By default `c.IP()`will return the Remote IP from the TCP connection, this property can be useful if you are behind a load balancer e.g. _X-Forwarded-\*_.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              | `""`                  |
    70  | ReadBufferSize               | `int`                 | per-connection buffer size for requests' reading. This also limits the maximum header size. Increase this buffer if your clients send multi-KB RequestURIs and/or multi-KB headers \(for example, BIG cookies\).                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               | `4096`                |
    71  | ReadTimeout                  | `time.Duration`       | The amount of time allowed to read the full request, including the body. The default timeout is unlimited.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     | `nil`                 |
    72  | RequestMethods                  | `[]string`       | RequestMethods provides customizibility for HTTP methods. You can add/remove methods as you wish.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     | `DefaultMethods`                 |
    73  | ServerHeader                 | `string`              | Enables the `Server` HTTP header with the given value.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         | `""`                  |
    74  | StreamRequestBody            | `bool`                | StreamRequestBody enables request body streaming, and calls the handler sooner when given body is larger then the current limit.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               | `false`               |
    75  | StrictRouting                | `bool`                | When enabled, the router treats `/foo` and `/foo/` as different. Otherwise, the router treats `/foo` and `/foo/` as the same.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  | `false`               |
    76  | TrustedProxies               | `[]string`            | Contains the list of trusted proxy IP's. Look at `EnableTrustedProxyCheck` doc. <br /> <br /> It can take IP or IP range addresses. If it gets IP range, it iterates all possible addresses.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   | `[]string*__*`        |
    77  | UnescapePath                 | `bool`                | Converts all encoded characters in the route back before setting the path for the context, so that the routing can also work with URL encoded special characters                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               | `false`               |
    78  | Views                        | `Views`               | Views is the interface that wraps the Render function. See our **Template Middleware** for supported engines.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  | `nil`                 |
    79  | ViewsLayout                  | `string`              | Views Layout is the global layout for all template render until override on Render function. See our **Template Middleware** for supported engines.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            | `""`                  |
    80  | WriteBufferSize              | `int`                 | Per-connection buffer size for responses' writing.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             | `4096`                |
    81  | WriteTimeout                 | `time.Duration`       | The maximum duration before timing out writes of the response. The default timeout is unlimited.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               | `nil`                 |
    82  | XMLEncoder                   | `utils.XMLMarshal`    | Allowing for flexibility in using another XML library for encoding.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            | `xml.Marshal`         |
    83  
    84  ## NewError
    85  
    86  NewError creates a new HTTPError instance with an optional message.
    87  
    88  ```go title="Signature"
    89  func NewError(code int, message ...string) *Error
    90  ```
    91  
    92  ```go title="Example"
    93  app.Get("/", func(c *fiber.Ctx) error {
    94      return fiber.NewError(782, "Custom error message")
    95  })
    96  ```
    97  
    98  ## IsChild
    99  
   100  IsChild determines if the current process is a result of Prefork.
   101  
   102  ```go title="Signature"
   103  func IsChild() bool
   104  ```
   105  
   106  ```go title="Example"
   107  // Prefork will spawn child processes
   108  app := fiber.New(fiber.Config{
   109      Prefork: true,
   110  })
   111  
   112  if !fiber.IsChild() {
   113      fmt.Println("I'm the parent process")
   114  } else {
   115      fmt.Println("I'm a child process")
   116  }
   117  
   118  // ...
   119  ```