Jump to content

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?

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);

 

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;

 

Link to post
Share on other sites
  • Moderators

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)

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?

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).

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...