Sign in Join
FPDoc Onlineexperiment

HttpRouterBasicUsage topic

In unit HTTPRoute

Basic HTTP Router Usage

Declaration

pascal
HttpRouterBasicUsage

Description

The simplest way to use the HTTP router is through callback procedures. The router matches incoming requests to registered URL patterns and HTTP methods:

program BasicRouter;

{$mode objfpc}{$H+}

uses
  httproute, httpdefs;

procedure HandleHome(ARequest: TRequest; AResponse: TResponse);
begin
  AResponse.Content := 'Welcome to the homepage!';
end;

procedure HandleAPI(ARequest: TRequest; AResponse: TResponse);
begin
  AResponse.Content := 'API endpoint response';
end;

begin
  // Register routes
  HTTPRouter.RegisterRoute('/', rmGet, @HandleHome, False);
  HTTPRouter.RegisterRoute('/api', rmGet, @HandleAPI, False);

  Writeln('HTTP Router configured with routes:');
  Writeln('GET / - Homepage');
  Writeln('GET /api - API endpoint');
end.

An experiment, built from development snapshots. Not the official Free Pascal or Lazarus documentation.