</bugz loading .

Http responses in go fiber in 3 steps [ Golang ]

In the previous tutorial we have learnt how to return a string ["hello world"] as http response. Today we'll be going to dive deeper. Let's start !

Recap - Basic Fiber App

        package main

  import "github.com/gofiber/fiber/v2"

  func main() {
    app := fiber.New()

    app.Get("/", func(ctx *fiber.Ctx) error {
      return ctx.SendString("Hello, World!")
    })

    app.Listen(":6969")
  }
      

In the above code the ctx.SendString() function returns the string Hello, World! as response.

hello world image

Serving Static Files

Static files are files that don't change when your application is running.
Example : js files css files images


To serve static files you've to call app.Static() method in your main function.

        app.Static(PREFIX, DIRECTORY)
      

Given above is the format of the method Static. You have to replace the word PREFIX with "/" or whatever prefix you want & replace the word DIRECTORY with the name of the directory where the static files are present.


Example :

        app.Static("static","./static_files")
      

If i have a file named style.css in my static_files folder, i can load the file by sending a request to this url πŸ‘‡πŸΎ

        http://localhost:6969/static/style.css
      

The word static in the url is the prefix which we have mentioned in the method Static, and style.css is the name of the file in the static_files directory.

Rendering html files/templates

Fiber provides a Views interface to provide your own template engine.

What is a template engine?
A template engine is a specific kind of template processing module that exhibits all of the major features of a modern programming language.



Some of the fiber's template engines are :


Importing an engine
        import "github.com/gofiber/template/ENGINE_NAME"
       

Replace the word ENGINE_NAME with any template engine's name.


Initialising the engine
        engine := ENGINE_NAME.New("DIRECTORY", ".html")
       

Replace the word ENGINE_NAME with any template engine's name & the DIRECTORY with the name of the directory where the html files are present


Passing the engine to Fiber's Views
        app := fiber.New(fiber.Config{
        Views: engine,
    })
       

Rendering the template
        app.Get("/", func(c *fiber.Ctx) error {

        return c.Render("FILE_NAME", fiber.Map{
            "CONTEXT_NAME": "CONTEXT_VALUE",
            "CONTEXT_NAME": "CONTEXT_VALUE",
        })
    })
      

Replace the word FILE_NAME with the name of the file without the .html extension & Replace the word CONTEXT_NAME with context name & CONTEXT_VALUE with the context value.


Example
        return c.Render("student_details", fiber.Map{
            "student_name": "Steve",
        })
       

That's it!πŸŽ‰ We are done with the basic format of our template rendering app.


Here's an example of the app πŸ‘‡πŸΎ
main.go
        package main

import (
    "log"
    "github.com/gofiber/fiber/v2"
    "github.com/gofiber/template/html"
)

func main() {

    engine := html.New("./views", ".html")

    app := fiber.New(fiber.Config{
        Views: engine,
    })
    app.Get("/", func(c *fiber.Ctx) error {

        return c.Render("student_details", fiber.Map{
            "student_name": "Steve",
        })
    })

    log.Fatal(app.Listen(":6969"))
}
       

index/student_details.html
         <h1>{{.student_name}}</h1>
       

And now if you browse http://localhost:6969/ in your browser, you'll see something like thisπŸ‘‡πŸΎ

hello steve image fiber
That's it for today! Happy Coding ✌🏾