When someone opens a URL served by your Daraja program, the request passes through a few building blocks before your code runs, and the answer travels back the same way. The left column shows what each block does for you. The right column shows the line of Pascal that sets it up.
A browser, a script or another program asks for http://127.0.0.1/tutorial/hello.
curl http://127.0.0.1/tutorial/hello
Waits on a network port, receives each request and reads it. This part is built on Indy, the networking library that Daraja uses under the hood.
Server := TdjServer.Create(80);
One server can host several apps side by side. The first part of the path, /tutorial, decides which app gets the request.
Server.Add(Context); Server.Start;
Groups everything that lives under one path, here /tutorial: which URL goes to which piece of your code, and settings the app shares, such as visitor sessions.
Context := TdjWebAppContext.Create('tutorial');
Looks at the rest of the path, /hello, and finds the class you registered for it. Patterns can be an exact path, a whole folder such as /api/*, or a file type such as *.html.
Context.Add(THelloWorldResource, '/hello');
A filter wraps around your code, so it can do something first (check a login, write a log line) and something afterwards (change the page it produced). Filters apply to many URLs at once.
Context.Add(TResponseHtmlFilter, '*.html'); procedure TResponseHtmlFilter.DoFilter; begin // before: runs first Chain.DoFilter(Context, Request, Response); // after: change the answer here end;
A request always has a type, called its method. Opening a page is GET; submitting a form is usually POST. Daraja calls the matching procedure, OnGet or OnPost, and you fill in the answer.
type THelloWorldResource = class(TdjWebComponent) public procedure OnGet(Request: TdjRequest; Response: TdjResponse); override; end; procedure THelloWorldResource.OnGet(...); begin Response.ContentText := 'Hello, World!'; Response.ContentType := 'text/plain'; end;
Whatever you put into Response travels back through any filters and is sent to the visitor. You never deal with the network yourself.
HTTP/1.1 200 OK Content-Type: text/plain Hello, World!