Jump to content
IElite

Endpoints & Resources ?

Recommended Posts

How do i create an endpoint/resource and call one of the HTTP server functions/verbs (with the endpoint/resource)?

I do not see anything in the featured demos that show this.

When using JS/Node.js and using Express, I would normally do

var express = require('express')
var app = express()

app.get('/api/programs', function (req, res) {
  //
});

app.delete('/api/programs/:id', function (req, res) {
  //
});


So, when I create a new SMART NodeJS project, where do I do declare the endpoint/resource?

unit Unit1;

interface

uses
  System.Types, System.Types.Convert, System.Objects, System.Device.Storage,
  System.Streams, System.Reader, System.Writer, System.JSON, System.IOUtils,
  SmartNJ.System, SmartNJ.Device.Storage, SmartNJ.Application,
  SmartNJ.Server.Http;

type
  TNodeProgram = class(TObject)
  private
    FServer:    TNJHTTPServer;
  protected
    procedure   HandleServerStarted(Sender: TObject);
    procedure   HandleRequest(Sender: TObject; const Request: TNJHttpRequest;
                const Response: TNJHttpResponse);
  public
    procedure   Execute;
    constructor Create; virtual;
    destructor  Destroy; override;
  end;

implementation

constructor TNodeProgram.Create;
begin
  inherited Create;
  FServer := TNJHTTPServer.Create;
  FServer.Port := 1881;
  FServer.OnAfterServerStarted := HandleServerStarted;
  FServer.OnRequest := HandleRequest;
end;

destructor TNodeProgram.Destroy;
begin
  FServer.free;
  inherited;
end;

procedure TNodeProgram.Execute;
begin
  // Start the server
  FServer.Start();
end;

procedure TNodeProgram.HandleServerStarted(Sender: TObject);
begin
  writelnF('Server listening on port %d', [FServer.port]);
end;

procedure TNodeProgram.HandleRequest(Sender: TObject; const Request: TNJHttpRequest;
  const Response: TNJHttpResponse);
begin
  // Just end the request by sending the headers back as a string
  response.&End(request.Headers.ToString());
end;


end.

 

Share this post


Link to post
Share on other sites

yep, express has routing built in

If you're not using express, then its a matter of parsing the url yourself

//add unit NodeJS.url
//code based on simplehttpserver in featured demos

procedure TServer.HandleRequest(Sender: TObject; const Request: TNJHttpRequest; const Response: TNJHttpResponse);
var content: String;
begin
  //Example on how to read content data (for example data sent using POST.
  //TNJHttpRequest itself is actually a readable stream, which can be read
  //in many ways. Hence, we don't automatically read it before HandleRequest is triggered.

  var url_parts : variant := url.parse(request.Url);
  case url_parts.pathname of
    '/kkk' : begin console.log(url_parts.pathname); end;
  end;

  Request.Handle.on('data', procedure(data: variant)
    begin
      content := data;
    end);

  Request.Handle.on('end', procedure(data: variant)
    begin
      Response.&End('Headers: '+request.Headers.ToString()+#13#10+
                   'Content: '+Variant(Content));
      //These are not automatically freed after HandleRequest as that would
      //prevent reading of content data using async functions.
      Request.Free;
      Response.Free;
    end);
end;

http://127.0.0.1:1881/kkk will execute the case branch

 

side note : console.log() needs

type
  JConsole = class external 'Console'
  public
    procedure log(data: Variant);
  end;
var Console external 'console': JConsole;

(or maybe there is a unit for that)

Share this post


Link to post
Share on other sites

A lot of RTL work and especially bug fixes was done, but Jon Lennart did pretty much all the NodeJS work.
In one of his posts https://jonlennartaasenden.wordpress.com/2019/01/19/quartex-web-os-a-cloud-os-in-takes-form/
he said: "Also, fixed more bugs in the Smart RTL than I can count..." These fixes are available just for top supporters, you have to pay to become a patron.
My intuition is that a quick and deep overdue of the Smart NJ part is required!
 

Share this post


Link to post
Share on other sites

@lynkfs

 thanks, I see how you showed how to parse the Url and handle each case. thanks!

not sure I am understanding how data works?

I am getting "Content: undefined" in my browser.

Can you tell me how the data variant is used?

 

Share this post


Link to post
Share on other sites

I'm not up to scratch on node myself, but what I understand is that when the node http-server receives a (post or get) request from a client, the body of the request is read by the server in chunks. Every chunk-read triggers an ondata event, and usually it is there where these chunks need to be concatenated until the onend event happens (end of read of request body).

There is an example here

Share this post


Link to post
Share on other sites
11 minutes ago, lynkfs said:

I'm not up to scratch on node myself, but what I understand is that when the node http-server receives a (post or get) request from a client, the body of the request is read by the server in chunks. Every chunk-read triggers an ondata event, and usually it is there where these chunks need to be concatenated until the onend event happens (end of read of request body).

There is an example here

OH OK, Thanks for your help!

 

 

Share this post


Link to post
Share on other sites

As you see above, the http server demo also does that. It builds the post data in the content-string. When all the data is read, the end-event is triggered. At that point you know that you have all the post data. The code can easily be changed to build the content as a stream.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×