Jump to content
IElite

Send requests to Node.js server from client?

Recommended Posts

How do you send requests from client to server?

for example, if I have the example node project code

procedure TNodeProgram.HandleRequest(Sender: TObject; const Request: TNJHttpRequest;
  const Response: TNJHttpResponse);
  var content: String;
begin
  if (Request.Url = '/test') AND (Request.Method = 'POST') then
  begin
   //
  end;
end;

what UNIT in the RTL holds the classes for creating objects to make requests to the server?

if I had the following Button onClick event, how would I send a request to my server

procedure TForm1.W3Button1Click(Sender: TObject);
begin

end;

Do i use TW3HttpRequest from the  SmartCL.Net.Http.pas unit?

Share this post


Link to post
Share on other sites

Update

Maybe something like this (i found in forum)

 

var AResponse: string;
 var Req:= TW3HttpRequest.Create;

 Req.OnDataReady := procedure(Sender: TW3HttpRequest)
  begin
   AResponse := Sender.ResponseText;
  end;

 Req.RequestHeaders.Add("Content-Type","text/plain");
 Req.Open('GET', 'http://127.0.0.1:3000/test');
 Req.send;

 writeln(AResponse);

 

Share this post


Link to post
Share on other sites

How do I send response back?

procedure TNodeProgram.HandleRequest(Sender: TObject; const Request: TNJHttpRequest;
  const Response: TNJHttpResponse);
  var content: String;
begin
 writeln('Url: ' + Request.Url + ';Method: ' + Request.Method);
  if (Request.Url = '/test') AND (Request.Method = 'GET') then
  begin
   //retrieve Request data
   Request.Handle.on('data', procedure(data: variant)
    begin
     writeln('Request.Handle.onData');
      content:= data;
    end);

    Request.Handle.on('end', procedure(data: variant)
    begin
      //something here ?
      writeln('Request.Handle.onEnd');
      response.statusCode:= 200;
      response.&End('');
      Request.Free;
      Response.Free;
     end);
  end;
end;

 

Share this post


Link to post
Share on other sites

you probably need to check explicitly on pathname :

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

otherwise looks ok

Share this post


Link to post
Share on other sites

As far as I understand Node, for a simple http server the action is in the handlerequest proc. 

This procedure has a request and a response object, and the latter can be used to send data back to the client.

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

This should be able to be picked up in the responseText of the calling httprequest  

(There are some examples on the forum doing something similar using sockets, but your approach should work too)

Share this post


Link to post
Share on other sites

My ultimate goal is to do the following:

1.)  Deploy JSON file with Client. Includes data and file version number

{
    "Example": {
        'fileVersion': 1.01
        "data": [...]
    }
}

2.) On Client startup, if there is a connection to do so, send a request to the server to see if the file version has changed.

3) If file version has changed, send back the new file ? is this possible? would the file be able to be written on the client side?

Share this post


Link to post
Share on other sites

> 3) If file version has changed, send back the new file ? is this possible? would the file be able to be written on the client side?

If code is runs in a standard browser (not natified webApp) then I think you can only save it to localstorage (quite easy) and use from there or you can build service worker for caching (lot of work).

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

×