* Node HTTP Server demo

This commit is contained in:
michael 2020-02-21 15:15:20 +00:00
parent dddcdbb4d2
commit 8032ea166e
2 changed files with 135 additions and 0 deletions

View File

@ -0,0 +1,85 @@
<?xml version="1.0" encoding="UTF-8"?>
<CONFIG>
<ProjectOptions>
<Version Value="12"/>
<General>
<Flags>
<MainUnitHasCreateFormStatements Value="False"/>
<MainUnitHasTitleStatement Value="False"/>
<MainUnitHasScaledStatement Value="False"/>
<Runnable Value="False"/>
</Flags>
<SessionStorage Value="InProjectDir"/>
<Title Value="nodehttpdemo"/>
<UseAppBundle Value="False"/>
<ResourceType Value="res"/>
</General>
<BuildModes>
<Item Name="Default" Default="True"/>
</BuildModes>
<PublishOptions>
<Version Value="2"/>
<UseFileFilters Value="True"/>
</PublishOptions>
<RunParams>
<FormatVersion Value="2"/>
</RunParams>
<Units>
<Unit>
<Filename Value="nodehttpdemo.lpr"/>
<IsPartOfProject Value="True"/>
</Unit>
<Unit>
<Filename Value="../../packages/nodejs/nodejshttp.pp"/>
<IsPartOfProject Value="True"/>
</Unit>
<Unit>
<Filename Value="../../packages/nodejs/nodejsnet.pp"/>
<IsPartOfProject Value="True"/>
</Unit>
</Units>
</ProjectOptions>
<CompilerOptions>
<Version Value="11"/>
<Target FileExt=".js">
<Filename Value="nodehttpdemo"/>
</Target>
<SearchPaths>
<IncludeFiles Value="$(ProjOutDir)"/>
<UnitOutputDirectory Value="js"/>
</SearchPaths>
<Parsing>
<SyntaxOptions>
<AllowLabel Value="False"/>
<CPPInline Value="False"/>
<UseAnsiStrings Value="False"/>
</SyntaxOptions>
</Parsing>
<CodeGeneration>
<TargetOS Value="nodejs"/>
</CodeGeneration>
<Linking>
<Debugging>
<GenerateDebugInfo Value="False"/>
<UseLineInfoUnit Value="False"/>
</Debugging>
</Linking>
<Other>
<CustomOptions Value="-Jeutf-8 -Jminclude -Jirtl.js"/>
<CompilerPath Value="$(pas2js)"/>
</Other>
</CompilerOptions>
<Debugging>
<Exceptions>
<Item>
<Name Value="EAbort"/>
</Item>
<Item>
<Name Value="ECodetoolError"/>
</Item>
<Item>
<Name Value="EFOpenError"/>
</Item>
</Exceptions>
</Debugging>
</CONFIG>

View File

@ -0,0 +1,50 @@
program nodehttpdemo;
{$mode objfpc}
uses
nodejsapp, JS, Classes, SysUtils, nodeJS, nodejshttp, nodejsnet;
type
{ TMyApplication }
TMyApplication = class(TNodeJSApplication)
procedure doRun; override;
private
procedure doRequest(req: TNJSHTTPIncomingMessage; resp: TNJSHTTPServerResponse);
end;
procedure TMyApplication.doRequest(req : TNJSHTTPIncomingMessage; resp : TNJSHTTPServerResponse);
Var
S : string;
h : JSValue;
begin
resp.write('Hello World!'+sLineBreak);
resp.write('You asked for: '+req.URL+sLineBreak);
resp.write('You sent the following headers: '+sLineBreak);
for s in TJSObject.getOwnPropertyNames(req.headers) do
begin
H:=req.headers[S];
if jsTypeOf(H)='string' then
resp.Write('Header "'+S+'": '+String(H)+sLineBreak);
end;
resp.end_(); //end the response
end;
procedure TMyApplication.doRun;
begin
http.createServer(@DoRequest).listen(7770);
end;
var
Application : TMyApplication;
begin
Application:=TMyApplication.Create(nil);
Application.Initialize;
Application.Run;
Application.Free;
end.