Context
Every HTTP request to the .html file will generate a context object, which you can access by using the . symbol in the code.
The context object contains the following information:
-
- Current server environment configuration:
.Config
- Current server environment configuration:
-
- Current request information:
.Request
- Current request information:
-
- Some operation methods used to respond to requests:
.Response
- Some operation methods used to respond to requests:
.Config
The .Config object contains the environment and configuration information of all servers.
Field list:
.Config.Host:stringtype, the Host monitored by the current server.Config.Port:inttype, the port number currently monitored by the server.Config.ApiServer:stringtype, the server address requested by the API interface, see Initiate an internal request for details.Config.Env:stringtype, the name of the custom environment currently running, see custom environment for details.Strs:map[string]map[string]stringtype, resource packs for all languages. For example:.Strs.zh-HK.HELLO_WORLD_can get the translation value ofHELLO_WORLD_under thekeyof the Chinese language pack. See Internationalization for details
.Request
The .Request object contains information about the current request.
Field list:
.Request.Method:stringtype, the method currently requested. E.g.GET.Request.Proto:stringtype, the protocol of the current request. For example,HTTP/1.0
Method list:
.Response
The .Response object contains some operation methods of the response body
Method list
-
.SetStatusCode:func(int)function type, used to set the HTTP status code of the response body (if not set, the HTTP status code defaults to 200)Scenario case:
We need an article page to get the content of the article and return it according to the route
/a/{article-id}, and return a 404 status code if it is not obtained.1.First, we define the
gte.config.jsonconfiguration file: —{ "host": "localhost", "port": 8080, "routes": [{ "path": "/a/:id", "to": "/article.html" } ], "blackList": [ "/article.html" ] }A route is defined here:
/a/:id, and all requests conforming to this pattern will be processed by the/article.htmlfile. For example: a request for/a/my-first-articleis in line with the routing mode. After entering/article.html, we will regard the second half of the routemy-firsrt-articleas a name The route variable ofidis passed, and you can get the value ofidby way of.Request.GetParam "id".The
blackListhere is a route blacklist, which prohibits users from directly accessing thearticle.htmlfile through the/article.htmlroute.2.Next we start to write the
article.htmlfile: —<!-- article.html --> <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>-Article</title> </head> <body> <h1></h1> <div> </div> </body> </html> <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Article not found</title> </head> <body> 404 article not found: </body> </html>Here we use the
httpGetJsonfunction to initiate an internal interface request (you need to prepare a back-end interface service to receive the request), the incoming parameter is a URL, and this URL is made byprint "http:// localhost:8080/api/articles/" (.Request.GetParam "id")stitched together.printis a function for concatenating strings, the type isJsonResponse func({any}...), any type and any number of parameters can be passed in. Because the second parameter ofprintis to get the second half of the current request route through(.Request.GetParam "id"), the value obtained is"my-first-article", so the final splicing The result is:"http://localhost:8080/api/articles/my-first-article"
After initiating the request, we define a
$resvariable to receive the response data of thehttpGetJsonfunction. (The:=symbol is used to declare a variable and assign a value). Next, we judge the response bodyif eq $res.StatusCode 200if the status code of the interface response is 200, indicating that the request for the article content data is successful, then the specific article content data will be displayed.The file content is obtained through
$res.Data, which is a JSON object that contains the article title$res.Data.Titleand the article content$res.Data.Content.If the response HTTP status code is not 200, the content after
elseis displayed