Discussion in 'Learning Javascript' started by Kino, Jun 20, 2017.

  1. Kino

    Kino EIS Game Dev Veteran

    Likes Received:
    United States
    First Language:
    Primarily Uses:
    Node.js, as mentioned previously, has a lot of modules with different APIs. These APIs give you lots of functionality. Today, we're touching on HTTP & HTTPS.

    HTTP and HTTPS are protocols (Hypertext Transfer Protocol) used to transfer information over the internet. Your computer acts a client and then makes a request to a server for information -- information in the form of files.

    Files are what you interact with over HTTP, and each file has a content type defining what it's interpreted as on the internet. More importantly, those files are what makes up a website; a website is just an HTML file sitting on a computer accessed through a URL (uniform resource locator). All of these concepts will be important when dealing with the HTTP modules provided in Node.js.

    The HTTP Modules
    As mentioned, HTTP is a protocol for looking at files over the internet. Node.js, HTTP modules give you that power. Now, let's make a quick differentiation between HTTP and HTTPS.

    HTTP is for regular links; it has no inherent protection. HTTPS is for secure content; this is often seen on government websites or websites that require you to pay for services. The short version is that your information is protected on these websites. There are much more technical details, but this is the basic idea. Now, HTTP is a protocol for communicating over the internet via requests such as HEAD, PUT, DELETE, OPTIONS, CONNECT, POST, and GET. Today, we're focusing on the GET method; the most used method.

    HTTP Request: GET
    HTTP focuses on client and server model, so the client or multiple clients make requests to the server. Here's an example:


    The GET request gets information from the URL (web address like provided. Then GET downloads that information from the URL. Then, you can down a file from! Now, in Node.js there is a simple get method attached to the HTTP modules for that purposes.

    Here's an example of a get request to my website:

    Here's an example output:


    This request goes to my website using the URL provided, downloads the data as a string of information by setting the encoding to 'utf8' in the callback function. Finally, information is processed in our two event listeners 'data' and 'end'. The 'data' event gives us data that we can process every time it's called with our callback function. The 'end' event tells us when the file is done being processed. The HTTPS get method operates in a similar way. The concept of events haven't been covered yet, but will be covered in another topic. In short, an event is a listener attached to a function that signals when a certain task is complete. That's a basic get request. The important thing to note that data is read in as chunks to a stream; streams allow you to take in data piece by piece instead of all at once.

    Hopefully, this helps you. Later, I'll cover events in Node.js and why they're the backbone of Node.js.
    Clock Out likes this.

Share This Page