nodehttpdemo.lpr 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. program nodehttpdemo;
  2. {$mode objfpc}
  3. uses
  4. nodejsapp, JS, Classes, SysUtils, nodeJS, node.http, node.net;
  5. type
  6. { TMyApplication }
  7. TMyApplication = class(TNodeJSApplication)
  8. procedure doRun; override;
  9. private
  10. procedure doRequest(req: TNJSHTTPIncomingMessage; resp: TNJSHTTPServerResponse);
  11. end;
  12. procedure TMyApplication.doRequest(req : TNJSHTTPIncomingMessage; resp : TNJSHTTPServerResponse);
  13. Var
  14. S : string;
  15. h : JSValue;
  16. begin
  17. resp.write('Hello World!'+sLineBreak);
  18. resp.write('You asked for: '+req.URL+sLineBreak);
  19. resp.write('You sent the following headers: '+sLineBreak);
  20. for s in TJSObject.getOwnPropertyNames(req.headers) do
  21. begin
  22. H:=req.headers[S];
  23. if jsTypeOf(H)='string' then
  24. resp.Write('Header "'+S+'": '+String(H)+sLineBreak);
  25. end;
  26. resp.end_(); //end the response
  27. end;
  28. procedure TMyApplication.doRun;
  29. begin
  30. http.createServer(@DoRequest).listen(7770);
  31. end;
  32. var
  33. Application : TMyApplication;
  34. begin
  35. Application:=TMyApplication.Create(nil);
  36. Application.Initialize;
  37. Application.Run;
  38. Application.Free;
  39. end.