Webserver example from wiki
lazarus / 38-Networking and Web Programming
----------------------------------------------------------------------
kirchfritz · 2024-01-10 17:24
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][+][-]window.onload = function(){var x1 =
document.getElementById("main_content_section"); if (x1) { var x =
document.getElementsByClassName("geshi");for (var i = 0; i procedure
route1(aReq: TRequest; aResp: TResponse);begin
Writeln(DateTimeToStr(now),' defaultEndpoint');
aResp.content:='
Route 1 The
Default
';end;
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][+][-]window.onload = function(){var x1 =
document.getElementById("main_content_section"); if (x1) { var x =
document.getElementsByClassName("geshi");for (var i = 0; i program
project1; {$mode objfpc}{$H+} uses {$ifdef UNIX} cthreads, cmem,
{$endif} sysutils, classes, fphttpapp, httpdefs,
httproute; procedure route1(aReq: TRequest; aResp: TResponse);begin
Writeln(DateTimeToStr(now),' defaultEndpoint');
aResp.content:='Route 1 The
Default
';end; procedure route2(aReq: TRequest;
aResp: TResponse);begin aResp.content:='Route
2
'end; begin HTTPRouter.registerRoute('/',
@route1, true); HTTPRouter.registerRoute('/route2', @route2,false);
Application.port := 8080; Application.threaded := true;
Application.initialize; Application.run;end.
----------------------------------------------------------------------
zeljko · 2024-01-10 20:18
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 · 2024-01-11 12:39
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][+][-]window.onload = function(){var x1 =
document.getElementById("main_content_section"); if (x1) { var x =
document.getElementsByClassName("geshi");for (var i = 0; i
HTTPRouter.registerRoute('/', @route1, false); and it shows only one
item in terminal