iisilikon.blogg.se

Nodejs http client example
Nodejs http client example






  1. #NODEJS HTTP CLIENT EXAMPLE INSTALL#
  2. #NODEJS HTTP CLIENT EXAMPLE CODE#

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.

nodejs http client example

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.

nodejs http client example

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.

nodejs http client example

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.

  • headers - an object containing response headers, a specific header can be accessed using the get() function.
  • ok - equals true if status is a 2xx status code (a successful request).
  • status and statusText - contain information about the HTTP status code.
  • json() - parses the response body into a JSON object, and throws an error if the body can't be parsed.
  • text() - returns the response body as a string.
  • The function returns a Response object that contains useful functions and information about the HTTP response, such as: The optional options parameter is used when we want to use fetch() for anything other than a simple GET request, but we will talk about that more in-depth later. It has to be an absolute URL or the function will throw an error. The url parameter is simply the direct URL to the resource we wish to fetch. To install a specific version of the module, you can use npm: $ npm install Īs previously mentioned, the fetch() function in the node-fetch module behaves very similarly to the native window.fetch() function. Https.Note: The API between node-fetch 3.0 and 2.0 is the same, just the import differs. SecureOptions: constants.SSL_OP_NO_SSLv2 | constants.SSL_OP_NO_SSLv3 | constants.SSL_OP_NO_TLSv1 //| constants.SSL_OP_NO_TLSv1_1 Openssl x509 -req -days 9999 -in csr.pem -signkey key.pem -out cert.pemĬonst constants = require('crypto').constants Openssl req -new -key key.pem -out csr.pem

    nodejs http client example

    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.








    Nodejs http client example