Helpers functions for web development and a web router:
$router:=tricho.router()
$router.get("/hello";"Hello world")
$router.post("/create";Formula($2.status(201).download("path/of/file"))
$router.get("/employee";Formula($2.render($templateFile;$employeeObject)))
...
Mainly wrapper to use C_OBJECT
- WebGetHTTPHeaders
- WebSetHTTPHeaders
- WebGetVariables: return an object of variables
- WebSendObject: send object or collection as JSON text (using
JSON Stringify
) - WebSendFile: send a
File
A router allow you define entry points to respond to HTTP requests.
Typically you provide the URL path, the HTTP method and the code to execute when matching.
$router:=tricho.router()
$router.get("/hello";"Hello world")
...
For test purpose you could create it at each client request but for efficiency in production mode cache it into a variable
In On Web Connection
you could handle all request using code:
$router.handle($1;$2;$3;$4;$5;$6)
You can choose one http method(GET, POST, PUT, ...) or all methods
$router.get("/hello";"This is a GET")
$router.post("/hello";"This is a POST")
$router.all("/hello";Formula("This is a "+$1.method))
Last parameters is the data to return to the HTTP client.
If you use a formula,
- the code could be dynamic ie. executed each times
- you can call an other methods to manage response
- you receive
- as $1 a context/request object with some useful features (to get headers, variables, ...)
- as $2 a reponse builder to be able to change status code, provoque a file download, add headers/cookies, etc..
If you return
- an object or a collection, it will be JSON stringifyed
- a
File
, it will be send as blob (with mime type according to file extension)
You can define parameters in route using :
, for instance to get the employee id
$router.get("/employee/:id";Formula(ProceedEmployeeData($1.params.id)))
then in HTTP client, you could access the resource using path /employee/12
A class must conform to some parameters and functions, then you can register as follow
$router.register(cs.YourRoute.new())
The class must defined the path
and methods
attributes.
Class constructor
This.methods:=New collection(HTTPMethod .GET)
This.path:="/a/class/path"
and must define a function to return the data.
Function respond
C_VARIANT($0)
C_OBJECT($1) // $context
$0:="Hello" // Return a String, an Object(JSON), 4D.File...
alteratively you can defined function by HTTP method if you do not defined methods
attribute
Function get
$0:="Hello"
Function post
$0:=New object("success";True)
Handler
is an alternative to Router
; it allow to register other methods to split and factorize your code used in On Web Connection
According to the request context (path, HTTP method, parameters) the handler must handle or not the request. If one handler respond, we stop.
$handler:=tricho. handler()
each time in
On Web Connection
or only one time inOn Startup
for instance
$handler.register(Formula(MyMethodToRespond(This)))
You can also use a class wich contains handle
function which True
if request handled.
Function handle
If ($1.path="/dayNumber")
WEB SEND TEXT(String(Day number(Current date)))
$0:=True // handled
Else
$0:=False // ignore request
End if
In On Web Connection
If ($handler.handle($1;$2;$3;$4;$5;$6))
// handled
Else
// other code like http 404 if not handled
End if
Router is inspired by numerous packages of different languages such as Flask for python, express for javascript, etc...
- Name come from Trichobothria