HttpServer example
pony5551 opened this issue · 3 comments
pony5551 commented
Please give an example of HttpServer, thank you
vovach777 commented
program ex08_httpServer;
{$APPTYPE CONSOLE}
{$R *.res}
uses
System.SysUtils,
np.core,
np.HttpServer,
np.Buffer;
procedure main(const addr: string; port: word);
var
server : IHttpServer;
begin
server := THttpServer.Create(addr,port,false,'','');
WriteLn('Listen: ', addr,':',port);
server.setOnRequest(
procedure (req : IHttpRequest; resp: IHttpResponse )
begin
if SameText( req.Path, '/exit') then
setImmediate(
procedure
begin
loop.terminate;
end);
resp.writeHeader(200);
resp.addHeader('Server','Node.pas example');
resp.addHeader('Content-Type', 'text/html; charset=utf-8');
resp.finish(
Buffer.Create(
Format('<h1>Hello!</h1><p>PATH: %s</p><h2>Request headers</h2><pre>%s</pre>----<br><b>/exit</b> to close server',
[req.Path, req.Headers.ToString]))
);
end );
end;
begin
try
loop.setImmediate(
procedure
begin
main('127.0.0.1',9999);
end);
loopHere;
WriteLn('Terminated');
except
on E: Exception do
Writeln(E.ClassName, ': ', E.Message);
end;
end.
vovach777 commented
HTTPS
server := THttpServer.Create(addr,port,true,'server.cert','server.key');
pony5551 commented
thanks