Jump to content
Sign in to follow this  
IElite

New to Node

Recommended Posts

As the title says, I am new to node and I am trying to follow this 4 part tutorial. Starting with the Part I: Hello World

However, I am not seeing what I should

e.g. My output says it is listening on 1881, instead of 80.  Why?

Quote

OUTPUT MESSAGES
[info] Program starting
24 Oct 10:12:27 - Server listening on port 1881

also, when I go to the address in my web browser:
http://127.0.0.1:1881/

I do not see Hello World, instead, I get something like this:

Chrome

{"host":"127.0.0.1:1881","connection":"keep-alive","upgrade-insecure-requests":"1","user-agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36","accept":"text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8","accept-encoding":"gzip, deflate, br","accept-language":"en-US,en;q=0.9,la;q=0.8"}

IE

{"user-agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.140 Safari/537.36 Edge/17.17134","accept-language":"en-US","accept":"text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8","upgrade-insecure-requests":"1","accept-encoding":"gzip, deflate","host":"127.0.0.1:1881","connection":"Keep-Alive"}

 

Share this post


Link to post
Share on other sites

If you're using the 'featured demos / node.js / http server example' project, then the output you're getting is correct.

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

its just the headers you get returned.

Share this post


Link to post
Share on other sites
1 hour ago, lynkfs said:

If you're using the 'featured demos / node.js / http server example' project, then the output you're getting is correct.


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

its just the headers you get returned.

No, I am not using the featured demo, I am following along with the tutorial described in my post

Share this post


Link to post
Share on other sites

Nodejs has changed a bit since the tutorial was written.

If you look at the code that is generated when you make a nodejs-app:

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

You can see here that the post 1881 is set here. Port 80 is not used as default as many do run a http server on their computer on port 80. So using another port is a good way to avoid conflicts.

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;

Here you can see how the default is to return the headers. If you want your "Hello world!", just do:

response.&End("Hello world!");

 

Share this post


Link to post
Share on other sites


I would like to access a file (High Scores) on a website. This is a text file with a list of high scores.  I presume it would be JSON.

{
  "highscores": [
    {
      "name": "John Doe",
      "Score": 100
    },
    {
      "name": "Ipsum lorem",
      "Score": 50
    },    
  ]
}


In my local app, i would like to display the high scores

So, I would need to fetch the file from the website using an HTTP Request

fHttp.Get('https://www.mywebsite.com/myfolder/highscores.json');

this seems easily enough 

Quote

Question 1: How do determine if there is a connection?
Question 2: How do you test for a successful fetch?

In my App, I would store the values in Memory

 fHighScores: String;
 gHighScores: Variant

procedure TMyGame.HandleHttpDataReady(Sender:TW3HttpRequest);
begin
  fHighScores:= Sender.ResponseText;
  gHighScores:= JSON.parse(fHighScores);
end;


Then test whether the user's score is greater. If so, place users name and score into memory and then save the new data structure back out to the website.

This is the part I need help with.


I am assuming I would need Node.js  for this?

I am NOT a javascript developer (hence SMS), let alone node.

Can someone help me with where to begin to do this?

Have you done something similar?

What is the conduit between the client side and the server side? How to make them talk to each other?

Possible, the server publishes methods that take params? Client sends some sort of request to the server with the method name, and params attached, and then the server can invoke it.????

We need more Node examples for simple processes such as this  :)

Thankx!

Share this post


Link to post
Share on other sites

Actually, the communication part between a NodeJS server and a Smart client app is pretty easy.

Client: You read data from the server using simple http/https get. When you want to write something to the server, you do it with http/https post.

Server: You can start with the NodeJS http server example. When your client sends a get or post, it comes to TServer.HandleRequest where you handle the request.

You can start by adding some WriteLn to TServer.HandleRequest to print out what the headers and the content is. Then you can send GET and POST requests to it by using a browser (http://localhost:1881/whatever/url/you/want/to/send/it?params=whatever) or curl. With Curl you can easily send post commands. For example:

curl -i -X POST -H 'Content-Type: application/json' -d '{"score": "Player": "10000"}' http://localhost:1881

When you can see what's going on in the server, you can also make the client app and start sending those http get/post with TW3HttpRequest (SmartCL.Net.Http)

I have done this earlier on the client side with Smart Mobile Studio and it worked nicely. There were hundreds of thousands of users on iOS and Android. The server was in Delphi but it can really be written in any language. I'd probably write my next server in Smart Mobile Studio using NodeJS.

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
Sign in to follow this  

×