Hello World in fpWeb!
lazarus / 38-Networking and Web Programming
----------------------------------------------------------------------
codezero · 2022-12-04 00:55
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][+][-]window.onload = function(){var x1 =
document.getElementById("main_content_section"); if (x1) { var x =
document.getElementsByClassName("geshi");for (var i = 0; i 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 := 'Hello,
World!'; Handled := true ;end; initialization
RegisterHTTPModule('TFPWebModule1', TFPWebModule1);end.
----------------------------------------------------------------------
Leledumbo · 2022-12-04 03:00
Ensure you have:
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
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 [Select][+][-]window.onload = function(){var x1 =
document.getElementById("main_content_section"); if (x1) { var x =
document.getElementsByClassName("geshi");for (var i = 0; i uses
fphttpapp, httpdefs, httproute;procedure DoHello(ARequest:TRequest;
AResponse : TResponse);begin AResponse.Content:='
Hell
o,World!
'end; begin HTTPRouter.RegisterRoute('*',
@DoHello); Application.Port := 9000; Application.Initialize;
Application.Run;end.
----------------------------------------------------------------------
egsuh · 2022-12-04 06:50
@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 · 2022-12-04 23:44
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.
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 · 2022-12-05 06:15
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][+][-]window.onload = function(){var x1 =
document.getElementById("main_content_section"); if (x1) { var x =
document.getElementsByClassName("geshi");for (var i = 0; i 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.
----------------------------------------------------------------------
nummer8 · 2022-12-05 08:11
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 · 2022-12-06 06:41
Quote from: egsuh on December 05, 2022, 07:15:48 amBut 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.