Jeff Goes
Jeff Goes

Reputation: 309

Need some clarification on nodejs concepts

I am starting to learn more about how this "web world" works and that's why I am taking the free code camp course. I already took front-end development and I really enjoyed it. Now I am on the back end part. The back end is much more foggy for me. There are many things that I don't get so I would hope that someone could help me out.

First of all I learned about the get method. so I did:

var http = require('http');

and then made a get request:

http.get(url, function callBack(response){
   response.setEncoding("utf8");
   response.on("data", function(data){
      console.log(data);
   });
});

Question 1) So apparently this code "gets" a response from a certain URL. but What response? I didn't even ask for anything in particular.

Moving on... The second exercise asks us to listen to a TCP connection and create a server and then write the date and time of that connection. So here's the answer:

var server = net.createServer(function listener (socket){
   socket.end(date);
});
server.listen(port);

Question 2) Okay so I created a TCP server with net.createServer() and when the connection was successful I outputted the date. But where? What did actually happen when I put date inside of socket.end()?

Last but not least... in the last exercise I was told to create an HTTP server (what?) to server a text file for every time it receives requests, and here's what I did:

var server = http.createServer(function callback(request, response){
   var read = fs.createReadStream(location);
   read.pipe(response);
});
server.listen(port);

Question 3) a) Why did I have to create an HTTP server instead of a regular TCP? what's the difference? b)what does createReadStream do? c) What does pipe() do?

If someone could help me, trying to make the explanation easier would help me a lot since I am, as you can see, pretty dumb on this subject.

Thank you a lot!

Upvotes: 1

Views: 65

Answers (2)

JORDANO
JORDANO

Reputation: 724

  1. For this answer, you need to understand two things. An IP address is the numeric value of a website, it's the address to the server pointing to the site. A domain name is a conversion from IP to a NAMED system which allows humans an easier way to see the names of websites, so instead of typing numbers for websites, like 192.168.1.1, we can now just type names (www.hotdog.com). That's what your get request is doing, it's requesting the site.

  2. socket.end is a method you're calling. socket.end "Half-closes the socket. i.e., it sends a FIN packet. It is possible the server will still send some data" from the nodejs.org docs, so basically it half closes your socket at the parameter you're sending in, which is todays current date.

  3. HTTP is hyper text transfer protocol, TCP (transmissioncontrol protocol) is a link between two computers

3a HTTP is for browsers, so that's why you did it, for a web page you were hosting locally or something.

3b createreadstream() Returns a new ReadStream object. (See Readable Stream).

Be aware that, unlike the default value set for highWaterMark on a readable stream (16 kb), the stream returned by this method has a default value of 64 kb for the same parameter.

3c pipe: The 'pipe' event is emitted when the stream.pipe() method is called on a readable stream, adding this writable to its set of destinations.

Upvotes: 0

Mark
Mark

Reputation: 92440

This is a little broad for Stackoverflow which favors focused questions that address specific problems. But I feel your pain, so…

Questions 1: Http.get is roughly equivalent to requesting a webpage. The url in the function is the page you are requesting. The response will include several things like the HTTP response code, but also (most importantly) the content of the page, which is what you are probably after. On the backend this is normally used for hitting APIs that get data rather than actual web pages, but the transport mechanism is the same.

Question 2: When you open a socket, you are waiting for someone else to request a connection. (The way you do when you use http.get(). When you output data you are sending them a response like the one you received in question 1.

Question 3: HTTP is a higher level protocol than TCP. This basically means it is more specific and TCP is more general (pedants will take issue with that statement, but it's an easy way to understand it). HTTP defines the things like GET and POST that you use when you download a webpage. Lower down in the protocol stack HTTP uses TCP. You could just use TCP, but you would have to do a lot more work to interpret the requests that come in. The HTTP library does that work for you. Other protocols like FTP also use TCP, but they are different protocol than HTTP.

Upvotes: 1

Related Questions