Forum > Networking and Web Programming
Hello World in fpWeb!
codezero:
I'm writing my first hello world but it gives me the following error:
https://prnt.sc/sV46CenhRoyb
error is triggered when from the browser I call ip:port
what can i do to fix it, of course if i press f8 with debugging in the browser nothing comes out
here is the code used
--- 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";}};} --- unit Unit1; {$mode objfpc}{$H+} interface uses SysUtils, Classes, httpdefs, fpHTTP, fpWeb; type { TFPWebModule1 } TFPWebModule1 = class(TFPWebModule) procedure test(Sender: TObject; ARequest: TRequest; AResponse: TResponse; var Handled: Boolean); private public end; var FPWebModule1: TFPWebModule1; implementation {$R *.lfm} { TFPWebModule1 } procedure TFPWebModule1.test(Sender: TObject; ARequest: TRequest; AResponse: TResponse; var Handled: Boolean);begin AResponse.Content := '<html>Hello, World!</html>'; Handled := true ;end; initialization RegisterHTTPModule('TFPWebModule1', TFPWebModule1);end.
Leledumbo:
Ensure you have:
--- 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";}};} ---Application.LegacyRouting := true; in your project's .lpr file. Please follow my tutorial. I suggest to no longer use webmodule based and instead do the pure code routing instead, much simpler and more maintainable. This is literally all you need to make your hello world, no form designer or lazarus project required:
--- 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";}};} ---uses fphttpapp, httpdefs, httproute;procedure DoHello(ARequest:TRequest; AResponse : TResponse);begin AResponse.Content:='<html><body><h1>Hello,World!</h1></body></html>'end; begin HTTPRouter.RegisterRoute('*', @DoHello); Application.Port := 9000; Application.Initialize; Application.Run;end.
egsuh:
@Leledumbo
Can you add another example that separates procedures of route and route2 of webserver example into another unit in your tutorial? I know how to do now, but in the beginning I had some difficulty in figuring out the working mechanism of new routing system. And separating units has advantages in later maintenance, especially when there are many procedures, and when we try to access a datamodule that accesses database.
Of course this is not a request... just my suggestion. Acceptance is up to you.
Leledumbo:
--- Quote from: egsuh on December 04, 2022, 07:50:44 am ---@Leledumbo
Can you add another example that separates procedures of route and route2 of webserver example into another unit in your tutorial? I know how to do now, but in the beginning I had some difficulty in figuring out the working mechanism of new routing system. And separating units has advantages in later maintenance, especially when there are many procedures, and when we try to access a datamodule that accesses database.
Of course this is not a request... just my suggestion. Acceptance is up to you.
--- End quote ---
The purpose of tutorial is to focus, modularization may hinder that purpose. After all, if you already understand modularization, the modular design would be up to you. Some people use a single unit that uses every other units providing the routes then register it there at initialization section, this helps maintenance by centralizing route registration in one place. Others instead, put the registration in each respective unit, making route registration modular and therefore, also helps maintenance. Now which one do you think is better and I should make a tutorial for? If you have an exact answer, you might need to learn modularization again, as there's no definitive better of the two, each has its own pros and cons.
egsuh:
I had written a web server application with old method. Later I found there are new routing methods. At first I wondered whether I should write whole unit again. But with some trial and errors, I found that I only have to redefine procedure headers, and old and new routings can co-exist.
Short mentions about these features would lessen the burden of trial and errors from developers, I think.
--- 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";}};} ---unit wmaq03c_laz; interface uses Classes, fpWeb, fpHTTP, httpdefs, HTTPRoute; // <== should be added type Twmaq03 = class(TFPWebModule) procedure HelloRequest(Sender: TObject; ARequest: TRequest; AResponse: TResponse; var Handled: Boolean); private procedure Hello(ARequest: TRequest; AResponse:TResponse); end; implementation // ......... initialization RegisterHTTPModule('s', Twmaq03); // this is old style HttpRouter.RegisterRoute('Hello', wmaq03.Hello); end.
Navigation
[0] Message Index
[#] Next page