Recent

Author Topic: Webserver example from wiki  (Read 1171 times)

kirchfritz

  • Jr. Member
  • **
  • Posts: 53
  • WIN10 LAZ 2.2.4 FPC 3.2.2
Webserver example from wiki
« on: January 10, 2024, 06:24:55 pm »
Hello,

I refer to the webserver example from the wiki
https://wiki.freepascal.org/fpWeb_Tutorial#Webserver_example

after successfully compiling i can enter
http://127.0.0.1:8080  in the browser
resulting in "route1"
So far so good.

Now I extended the example by using an additional writeln-instruction

Code: Pascal  [Select][+][-]
  1. procedure route1(aReq: TRequest; aResp: TResponse);
  2. begin
  3.   Writeln(DateTimeToStr(now),' defaultEndpoint');
  4.   aResp.content:='<html><body><h1>Route 1 The Default</h1></body></html>';
  5. end;
  6.  

As a Very strange result i recognized that procedure route1 is always called twice.
Whats wrong with my code?

Fritz

This is my complete program:
Code: Pascal  [Select][+][-]
  1. program project1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. uses
  6.   {$ifdef UNIX}
  7.     cthreads, cmem,
  8.   {$endif}
  9.   sysutils, classes,
  10.   fphttpapp, httpdefs, httproute;
  11.  
  12. procedure route1(aReq: TRequest; aResp: TResponse);
  13. begin
  14.   Writeln(DateTimeToStr(now),' defaultEndpoint');
  15.   aResp.content:='<html><body><h1>Route 1 The Default</h1></body></html>';
  16. end;
  17.  
  18. procedure route2(aReq: TRequest; aResp: TResponse);
  19. begin
  20.   aResp.content:='<html><body><h1>Route 2</h1></body></html>'
  21. end;
  22.  
  23. begin
  24.   HTTPRouter.registerRoute('/', @route1, true);
  25.   HTTPRouter.registerRoute('/route2', @route2,false);
  26.   Application.port := 8080;
  27.   Application.threaded := true;
  28.   Application.initialize;
  29.   Application.run;
  30. end.
  31.  
  32.  

zeljko

  • Hero Member
  • *****
  • Posts: 1602
    • http://wiki.lazarus.freepascal.org/User:Zeljan
Re: Webserver example from wiki
« Reply #1 on: January 10, 2024, 09:18:34 pm »
You must log request headers and complete call. Maybe because browser searches for /favicon.ico or similar call...
Try to access your webserver with curl and you'll see if it's called twice.

krolikbest

  • Full Member
  • ***
  • Posts: 247
Re: Webserver example from wiki
« Reply #2 on: January 11, 2024, 01:39:54 pm »
Hi,
try to build your project and launch it from an exe file instead in Lazarus using F9. Then it should show only one item in terminal
No, wrong, I did:
Code: Pascal  [Select][+][-]
  1. HTTPRouter.registerRoute('/', @route1, false);
and it shows only one item in terminal
« Last Edit: January 11, 2024, 01:46:29 pm by krolikbest »

 

TinyPortal © 2005-2018