Recent

Author Topic: Hello World in fpWeb!  (Read 591 times)

codezero

  • Newbie
  • Posts: 2
Hello World in fpWeb!
« on: December 04, 2022, 01:55:26 am »


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  [Select][+][-]
  1.  
  2.  
  3.  
  4. unit Unit1;
  5.  
  6. {$mode objfpc}{$H+}
  7.  
  8. interface
  9.  
  10. uses
  11.   SysUtils, Classes, httpdefs, fpHTTP, fpWeb;
  12.  
  13. type
  14.  
  15.   { TFPWebModule1 }
  16.  
  17.   TFPWebModule1 = class(TFPWebModule)
  18.     procedure test(Sender: TObject; ARequest: TRequest; AResponse: TResponse;
  19.       var Handled: Boolean);
  20.   private
  21.  
  22.   public
  23.  
  24.   end;
  25.  
  26. var
  27.   FPWebModule1: TFPWebModule1;
  28.  
  29. implementation
  30.  
  31. {$R *.lfm}
  32.  
  33. { TFPWebModule1 }
  34.  
  35. procedure TFPWebModule1.test(Sender: TObject; ARequest: TRequest;
  36.   AResponse: TResponse; var Handled: Boolean);
  37. begin
  38.     AResponse.Content     := '<html>Hello, World!</html>';
  39.     Handled := true ;
  40. end;
  41.  
  42. initialization
  43.   RegisterHTTPModule('TFPWebModule1', TFPWebModule1);
  44. end.  
  45.  

Leledumbo

  • Hero Member
  • *****
  • Posts: 8522
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: Hello World in fpWeb!
« Reply #1 on: December 04, 2022, 04:00:37 am »
Ensure you have:
Code: Pascal  [Select][+][-]
  1. Application.LegacyRouting := true;
  2.  
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  [Select][+][-]
  1. uses
  2.   fphttpapp, httpdefs, httproute;
  3. procedure DoHello(ARequest:TRequest; AResponse : TResponse);
  4. begin
  5.   AResponse.Content:='<html><body><h1>Hello,World!</h1></body></html>'
  6. end;
  7.  
  8. begin
  9.   HTTPRouter.RegisterRoute('*', @DoHello);
  10.   Application.Port := 9000;
  11.   Application.Initialize;
  12.   Application.Run;
  13. end.
  14.  

egsuh

  • Hero Member
  • *****
  • Posts: 1064
Re: Hello World in fpWeb!
« Reply #2 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.

Leledumbo

  • Hero Member
  • *****
  • Posts: 8522
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: Hello World in fpWeb!
« Reply #3 on: December 05, 2022, 12:44:07 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.
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

  • Hero Member
  • *****
  • Posts: 1064
Re: Hello World in fpWeb!
« Reply #4 on: December 05, 2022, 07:15:48 am »
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  [Select][+][-]
  1. unit wmaq03c_laz;
  2.  
  3. interface
  4.  
  5. uses
  6.   Classes,  fpWeb, fpHTTP, httpdefs,
  7.   HTTPRoute;   // <== should be added
  8.  
  9. type
  10.   Twmaq03 = class(TFPWebModule)
  11.        procedure HelloRequest(Sender: TObject; ARequest: TRequest;
  12.             AResponse: TResponse; var Handled: Boolean);
  13.  
  14.   private
  15.        procedure Hello(ARequest: TRequest; AResponse:TResponse);
  16.   end;
  17.  
  18.  
  19. implementation
  20.  
  21. // .........
  22.  
  23. initialization
  24.     RegisterHTTPModule('s', Twmaq03); // this is old style
  25.     HttpRouter.RegisterRoute('Hello', wmaq03.Hello);  
  26. end.

nummer8

  • Jr. Member
  • **
  • Posts: 93
Re: Hello World in fpWeb!
« Reply #5 on: December 05, 2022, 09:11:13 am »
You can find some examples

https://medium.com/@marcusfernstrm
https://www.youtube.com/channel/UCgPCV4NDWLKdnKCH-y6N6wg

There are story's about REST API.
If you change the JSON content with HTML content you have the examples you are asking for.
 

Leledumbo

  • Hero Member
  • *****
  • Posts: 8522
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: Hello World in fpWeb!
« Reply #6 on: December 06, 2022, 07:41:06 am »
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.
Assuming HelloRequest calls Hello (this is the flow I suggest, not the other way around), yes both can coexist, but one is mutually exclusive from another, as there's only one can be active at any given time through that LegacyRouting switch. It could be put in the tips and tricks section.

 

TinyPortal © 2005-2018