Forum > Networking and Web Programming

Webserver example from wiki

(1/1)

kirchfritz:
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  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---procedure route1(aReq: TRequest; aResp: TResponse);begin  Writeln(DateTimeToStr(now),' defaultEndpoint');  aResp.content:='<html><body><h1>Route 1 The Default</h1></body></html>';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  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---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:='<html><body><h1>Route 1 The Default</h1></body></html>';end; procedure route2(aReq: TRequest; aResp: TResponse);begin  aResp.content:='<html><body><h1>Route 2</h1></body></html>'end; begin  HTTPRouter.registerRoute('/', @route1, true);  HTTPRouter.registerRoute('/route2', @route2,false);  Application.port := 8080;  Application.threaded := true;  Application.initialize;  Application.run;end.  

zeljko:
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:
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  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---HTTPRouter.registerRoute('/', @route1, false); and it shows only one item in terminal

Navigation

[0] Message Index

Go to full version