First Route, API, and HTML Response
Create your first API, its handler, and HTML response in the Beego application.
We'll cover the following...
We'll cover the following...
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 routersimport ("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
API to the MainController
controller. The handler function is Hello()
for the HTTP method GET
. ...