...

/

First Route, API, and HTML Response

First Route, API, and HTML Response

Create your first API, its handler, and HTML response in the Beego application.

In this lesson, we will create a simple API that will be routed to a controller function, and a simple HTML template will be rendered as a response.

Defining the route

Let’s create a new API called GET /hello. All the APIs are listed in the file routers/router.go. We will add our API here and map it to its handler, a controller function.

Press + to interact
package routers
import (
"hello_beego/controllers"
"github.com/astaxie/beego"
)
func init() {
beego.Router("/", &controllers.MainController{})
beego.Router("/hello", &controllers.MainController{}, "get:Hello")
}

Line 11: A new route is created. The beego.Router() function is used to route the /hello ...