
We will retrieve fake user data from the JSONPlaceholder REST API. We'll get the entire HTML markup of the Google home page logged to the console: Fetching JSON Data From REST APIĪnother common use case for the node-fetch module is getting data using the REST API.
#NODEJS HTTP CLIENT EXAMPLE CODE#
If we run the code above from the console: $ node index.js Here we're waiting for the result of the previous conversion and printing it to the console. In this line, we're waiting to receive the response from the Google web server and converting it to text format. then() functions to help us manage the response and data from our request.

Because node-fetch is promise-based, we're chaining a couple of. The only parameter we've added to the fetch() function is the URL of the server we're making an HTTP request to. In the code above, we're loading the node-fetch module and then fetching the Google home page.

Let's make a simple GET request to Google's home page: fetch( '')
#NODEJS HTTP CLIENT EXAMPLE INSTALL#
Next, install node-fetch as shown above and add an index.js file. This will create a package.json file in the directory.

The node-fetch package allows you to do all of that.Ĭreate a directory for your project, cd into the directory and initialize a Node project with default settings: $ npm init -y You might want to retrieve text from the web server, a whole web page, or data from using REST API. There are two common use cases of fetching data from a web server.

Run the following from the command line to generate self-signed certificate. To create a https server, a certificate is needed. For example, if a server does not allow TLS1.0 and TLS1.1, then the client has to use TLS1.2ġ. The nodejs server can restrict which secure protocol is not accepted, and the client can choose which secure protocol to use when making a request to a server. This is a simple walk through for configuring TLS(Transport Layer Security) version in a nodejs server and client.
