Recent

Author Topic: fpweb and pas2js  (Read 2108 times)

krolikbest

  • Full Member
  • ***
  • Posts: 246
fpweb and pas2js
« on: April 21, 2021, 07:06:39 pm »
Hi,

let's say this simple code:
Code: Pascal  [Select][+][-]
  1. program project1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. uses
  6.    {$IFDEF UNIX}{$IFDEF UseCThreads}
  7.   cthreads,
  8.   {$ENDIF}{$ENDIF}
  9.   sysutils, fphttpapp, HTTPDefs, fpwebfile, httproute, fpWeb;
  10.  
  11. var
  12.   someVar, buff: String;
  13.  
  14. procedure Test(aReq: TRequest; aRes: TResponse);
  15. begin
  16.   someVar := aReq.QueryFields.Values['txt']; //get method
  17.   writeln(someVar);
  18.  
  19.  if somevar='12' then
  20.   begin
  21.     ARes.ContentType := 'text/html; charset=utf-8';
  22.     ARes.Contents.LoadFromFile(ExtractFilePath(ParamStr(0)) + 'project11.html'); //????????
  23.   end
  24.     else
  25.   begin
  26.     ss:='HOME';
  27.     aRes.Code:=200;
  28.     aRes.ContentType := 'text/html;charset=utf-8';
  29.     aRes.Content := '<html><head><meta http-equiv="refresh" content="3"></head><body>'+buff+ss+'</body></html>';
  30.     aRes.ContentLength := Length(aRes.Content);
  31.     aRes.SendContent;
  32.     buff:=somevar;
  33.   end;
  34. end;
  35.  
  36. begin
  37.   Application.Port:=8080;
  38.   HTTPRouter.RegisterRoute('/',@Test, true);
  39.   Application.threaded := true;
  40.   Application.Initialize;
  41.   Application.Run;
  42. end.
In the web browser:  http://192.168.1.4:8080/?txt=12
runs ok if someVar<>'12'. If someVar='12' then it should load project11.html (this is a file made in pas2js). But evidently doesn't. In Chrome Console I see
"Uncaught SyntaxError: Unexpected token '<'
?txt=12:13 Uncaught ReferenceError: rtl is not defined at ?txt=12:13"
If file project11.html doesn't contain javascript then it loads. Hints?

Regards,


ojz0r

  • Jr. Member
  • **
  • Posts: 58
Re: fpweb and pas2js
« Reply #1 on: April 21, 2021, 07:52:32 pm »
I had problems with external files in fpweb, what i did is to add:
Code: Pascal  [Select][+][-]
  1. RegisterFileLocation('your.file', ExtractFilePath(ParamStr(0)));
  2.  

Before:

Code: Pascal  [Select][+][-]
  1. HTTPRouter.RegisterRoute('/',@Test, true);
  2.  

Perhaps its the same issue?
Just trying to learn.

krolikbest

  • Full Member
  • ***
  • Posts: 246
Re: fpweb and pas2js
« Reply #2 on: April 21, 2021, 08:05:32 pm »
If you work in Linux there it won't be probably  issue with RegisterFileLocation, but I work presently in Win7 (Laz 2.0.10) and have issue with this routine. I had it earlier as well but can't recall where I read about this issue with  RegisterFileLocation..

PierceNg

  • Sr. Member
  • ****
  • Posts: 369
    • SamadhiWeb
Re: fpweb and pas2js
« Reply #3 on: April 22, 2021, 06:41:26 am »
In the web browser:  http://192.168.1.4:8080/?txt=12
runs ok if someVar<>'12'. If someVar='12' then it should load project11.html (this is a file made in pas2js). But evidently doesn't. In Chrome Console I see
"Uncaught SyntaxError: Unexpected token '<'
?txt=12:13 Uncaught ReferenceError: rtl is not defined at ?txt=12:13"
If file project11.html doesn't contain javascript then it loads. Hints?

What does project11.html look like? Is it that project11.html does not run the pas2js RTL?

Code: Javascript  [Select][+][-]
  1. <script>rtl.run()</script>


krolikbest

  • Full Member
  • ***
  • Posts: 246
Re: fpweb and pas2js
« Reply #4 on: April 22, 2021, 09:34:50 am »
If independently then Project11.html starts in the browser. But doesn't if I try to start it as described earlier.

egsuh

  • Hero Member
  • *****
  • Posts: 1266
Re: fpweb and pas2js
« Reply #5 on: April 22, 2021, 09:51:31 am »
There could be some problem with Project11.html. Check the encoding type of project11.html file, and whether no template replacement is working. Display the source of erroneous result on browser, and compare it with the original file (opened in text editor). If you see any difference, problem lies there.

PierceNg

  • Sr. Member
  • ****
  • Posts: 369
    • SamadhiWeb
Re: fpweb and pas2js
« Reply #6 on: April 22, 2021, 10:02:38 am »
If independently then Project11.html starts in the browser. But doesn't if I try to start it as described earlier.

project11.html and its assets like pas2js-transpiled Javascript are separate files. Could it be, when you load 'independently' whether from disk or via a web server, all assets are served, but your fpWeb application is however serving only project11.html and not the Javascript etc it references?

krolikbest

  • Full Member
  • ***
  • Posts: 246
Re: fpweb and pas2js
« Reply #7 on: April 22, 2021, 10:21:33 am »
In attachment project11.html, project11.js, I made just now in order to check on different machine and place :)

PierceNg

  • Sr. Member
  • ****
  • Posts: 369
    • SamadhiWeb
Re: fpweb and pas2js
« Reply #8 on: April 22, 2021, 11:34:51 am »
In attachment project11.html, project11.js, I made just now in order to check on different machine and place :)

Funny error.

I am on Linux. I added a RegisterFileLocation statement to main program and the program works as expected:

Code: Pascal  [Select][+][-]
  1. begin
  2.   Application.Port:=8080;
  3.   RegisterFileLocation('project11.js', ExtractFilePath(ParamStr(0)));
  4.   HTTPRouter.RegisterRoute('/',@Test, true);
  5.   Application.threaded := true;
  6.   Application.Initialize;
  7.   Application.Run;
  8. end.

krolikbest

  • Full Member
  • ***
  • Posts: 246
Re: fpweb and pas2js
« Reply #9 on: April 22, 2021, 11:47:47 am »
Yes, I know. Unfortunatelly i'm on Win7... That why I have some issue with RegisterFileLocation.

PascalDragon

  • Hero Member
  • *****
  • Posts: 5444
  • Compiler Developer
Re: fpweb and pas2js
« Reply #10 on: April 22, 2021, 01:28:44 pm »
Yes, I know. Unfortunatelly i'm on Win7... That why I have some issue with RegisterFileLocation.

You can either use FPC 3.2.2 RC1 which has that problem fixed or as a workaround you can pass a so called drive absolute path: if your relative path is .\files, your absolute pah is C:\projects\webtest\files then the drive absolute path is \projects\webtest\files assuming that your current directory is a folder on drive C:. You then need to pass /projects\webtest\files (note the first / instead of \) to RegisterFileLocation so that it doesn't adjust the path. To do this in code you need to use ExpandFileName, remove the first two characters (if they're an ASCII letter and a “:”) and change the \ at the now first position from a \ to a /.

krolikbest

  • Full Member
  • ***
  • Posts: 246
Re: fpweb and pas2js
« Reply #11 on: April 22, 2021, 02:06:51 pm »
Thanks. As soon as i'm on this prioject will do as you said.

 

TinyPortal © 2005-2018