github.com/gofiber/fiber/v2@v2.47.0/docs/extra/faq.md (about) 1 --- 2 id: faq 3 title: 🤔 FAQ 4 description: >- 5 List of frequently asked questions. Feel free to open an issue to add your 6 question to this page. 7 sidebar_position: 1 8 --- 9 10 ## How should I structure my application? 11 12 There is no definitive answer to this question. The answer depends on the scale of your application and the team that is involved. To be as flexible as possible, Fiber makes no assumptions in terms of structure. 13 14 Routes and other application-specific logic can live in as many files as you wish, in any directory structure you prefer. View the following examples for inspiration: 15 16 * [gofiber/boilerplate](https://github.com/gofiber/boilerplate) 17 * [thomasvvugt/fiber-boilerplate](https://github.com/thomasvvugt/fiber-boilerplate) 18 * [Youtube - Building a REST API using Gorm and Fiber](https://www.youtube.com/watch?v=Iq2qT0fRhAA) 19 * [embedmode/fiberseed](https://github.com/embedmode/fiberseed) 20 21 ## How do I handle custom 404 responses? 22 23 If you're using v2.32.0 or later, all you need to do is to implement a custom error handler. See below, or see a more detailed explanation at [Error Handling](../guide/error-handling.md#custom-error-handler). 24 25 If you're using v2.31.0 or earlier, the error handler will not capture 404 errors. Instead, you need to add a middleware function at the very bottom of the stack \(below all other functions\) to handle a 404 response: 26 27 ```go title="Example" 28 app.Use(func(c *fiber.Ctx) error { 29 return c.Status(fiber.StatusNotFound).SendString("Sorry can't find that!") 30 }) 31 ``` 32 33 ## How can i use live reload ? 34 35 [Air](https://github.com/cosmtrek/air) is a handy tool that automatically restarts your Go applications whenever the source code changes, making your development process faster and more efficient. 36 37 To use Air in a Fiber project, follow these steps: 38 39 1. Install Air by downloading the appropriate binary for your operating system from the GitHub release page or by building the tool directly from source. 40 2. Create a configuration file for Air in your project directory. This file can be named, for example, .air.toml or air.conf. Here's a sample configuration file that works with Fiber: 41 ```toml 42 # .air.toml 43 root = "." 44 tmp_dir = "tmp" 45 [build] 46 cmd = "go build -o ./tmp/main ." 47 bin = "./tmp/main" 48 delay = 1000 # ms 49 exclude_dir = ["assets", "tmp", "vendor"] 50 include_ext = ["go", "tpl", "tmpl", "html"] 51 exclude_regex = ["_test\\.go"] 52 ``` 53 3. Start your Fiber application using Air by running the following command in the terminal: 54 ```sh 55 air 56 ``` 57 58 As you make changes to your source code, Air will detect them and automatically restart the application. 59 60 A complete example demonstrating the use of Air with Fiber can be found in the [Fiber Recipes repository](https://github.com/gofiber/recipes/tree/master/air). This example shows how to configure and use Air in a Fiber project to create an efficient development environment. 61 62 63 ## How do I set up an error handler? 64 65 To override the default error handler, you can override the default when providing a [Config](../api/fiber.md#config) when initiating a new [Fiber instance](../api/fiber.md#new). 66 67 ```go title="Example" 68 app := fiber.New(fiber.Config{ 69 ErrorHandler: func(c *fiber.Ctx, err error) error { 70 return c.Status(fiber.StatusInternalServerError).SendString(err.Error()) 71 }, 72 }) 73 ``` 74 75 We have a dedicated page explaining how error handling works in Fiber, see [Error Handling](../guide/error-handling.md). 76 77 ## Which template engines does Fiber support? 78 79 Fiber currently supports 8 template engines in our [gofiber/template](https://github.com/gofiber/template) middleware: 80 81 * [Ace](https://github.com/yosssi/ace) 82 * [Amber](https://github.com/eknkc/amber) 83 * [Django](https://github.com/flosch/pongo2) 84 * [Handlebars](https://github.com/aymerick/raymond) 85 * [HTML](https://pkg.go.dev/html/template/) 86 * [Jet](https://github.com/CloudyKit/jet) 87 * [Mustache](https://github.com/cbroglie/mustache) 88 * [Pug](https://github.com/Joker/jade) 89 90 To learn more about using Templates in Fiber, see [Templates](../guide/templates.md). 91 92 ## Does Fiber have a community chat? 93 94 Yes, we have our own [Discord ](https://gofiber.io/discord)server, where we hang out. We have different rooms for every subject. 95 If you have questions or just want to have a chat, feel free to join us via this **>** [**invite link**](https://gofiber.io/discord) **<**. 96 97  98 99 ## Does fiber support sub domain routing ? 100 101 Yes we do, here are some examples: 102 This example works v2 103 ```go 104 package main 105 106 import ( 107 "log" 108 109 "github.com/gofiber/fiber/v2" 110 "github.com/gofiber/fiber/v2/middleware/logger" 111 ) 112 113 type Host struct { 114 Fiber *fiber.App 115 } 116 117 func main() { 118 // Hosts 119 hosts := map[string]*Host{} 120 //----- 121 // API 122 //----- 123 api := fiber.New() 124 api.Use(logger.New(logger.Config{ 125 Format: "[${ip}]:${port} ${status} - ${method} ${path}\n", 126 })) 127 hosts["api.localhost:3000"] = &Host{api} 128 api.Get("/", func(c *fiber.Ctx) error { 129 return c.SendString("API") 130 }) 131 //------ 132 // Blog 133 //------ 134 blog := fiber.New() 135 blog.Use(logger.New(logger.Config{ 136 Format: "[${ip}]:${port} ${status} - ${method} ${path}\n", 137 })) 138 hosts["blog.localhost:3000"] = &Host{blog} 139 blog.Get("/", func(c *fiber.Ctx) error { 140 return c.SendString("Blog") 141 }) 142 //--------- 143 // Website 144 //--------- 145 site := fiber.New() 146 site.Use(logger.New(logger.Config{ 147 Format: "[${ip}]:${port} ${status} - ${method} ${path}\n", 148 })) 149 150 hosts["localhost:3000"] = &Host{site} 151 site.Get("/", func(c *fiber.Ctx) error { 152 return c.SendString("Website") 153 }) 154 // Server 155 app := fiber.New() 156 app.Use(func(c *fiber.Ctx) error { 157 host := hosts[c.Hostname()] 158 if host == nil { 159 return c.SendStatus(fiber.StatusNotFound) 160 } else { 161 host.Fiber.Handler()(c.Context()) 162 return nil 163 } 164 }) 165 log.Fatal(app.Listen(":3000")) 166 } 167 ``` 168 If more information is needed, please refer to this issue [#750](https://github.com/gofiber/fiber/issues/750)