[fpc-pascal] Writing fpWeb app without Lazarus

Michael Van Canneyt michael at freepascal.org
Tue Jun 7 10:23:35 CEST 2016

On Sun, 5 Jun 2016, Mr Bee wrote:

> Hi all,
> I'm now learning how to use fpWeb. But for some reasons, I don't want to use Lazarus. Instead, I want to write the program using plain text editor or console text editor (like vim, nano, etc), in case I need to do it remotely. It's now just a simple Hello World program, so it must be simple enough to write the program as a single file without Lazarus. However, I failed. There must be something stupid that I forgot to write but I don't know what that is. So here's my code: program HelloWeb;
> {$MODE OBJFPC}{$H+}{$J-}
> uses  Classes, HTTPDefs, fpHTTP, fpCGI, fpWeb;
> type  THelloModule = class(TFPWebModule)    procedure HelloReq(Sender: TObject; ARequest: TRequest;                       AResponse: TResponse; var Handled: Boolean);  end;
> var  HelloModule: THelloModule;
> procedure THelloModule.HelloReq(Sender: TObject; ARequest: TRequest;                                AResponse: TResponse; var Handled: Boolean);begin  AResponse.ContentType := 'text/html;charset=utf-8';  AResponse.Content := '<html><body>Hello World!</body></html>';  Handled := true;end;
> begin  HelloModule := THelloModule.Create(nil);  HelloModule.ActionVar := 'm';  HelloModule.OnRequest := @HelloModule.HelloReq;
>   RegisterHTTPModule('THelloModule',THelloModule,true);
>   Application.Title := 'Hello World';  Application.Initialize;  Application.Run;end.
> It was compiled fine without any errors using -XXs -CX -O3 parameters. But when the executable is deployed and then accessed from the browser, it returns 500 internal server error. 
> I don't know what to do as I saw nothing is wrong.

What is the URL you used to access your app ?
What is the output in the browser ?

Then, never create an instance of a webmodule manually, always let the
framework create instances.

Also, a statement as

HelloModule := THelloModule.Create(nil);

is wrong, since it will attempt to stream the module. If you do want to
create modules manually, do
HelloModule := THelloModule.CreateNew(nil,0);

> That's the first problem. The second one is a question. I'd like to use fpWeb components in plain procedural way, without using the TApplication infrastructure, especially for simple CGI apps/services. Is it possible? Any advices?

No, this is not possible. 
It was explicitly designed to use classes in a RAD environment.
It is perfectly possible to drop the RAD part and use the classes as-is, but
it is not possible not to use the classes.

> I'm using a Linux Ubuntu 14.04 machine with Apache web server. I believe the problem has nothing to do with the OS nor the web server because my plain Pascal CGI (without using fpWeb) is running just fine.

Without more information, it is hard to say.

There are multiple examples in the FPC source tree; 
Why don't you use those as a starting point ?


More information about the fpc-pascal mailing list