Node.js: Making HTTPCIient requests

Another way to mitigate computationally intensive code is to push the calculation to a backend process. To explore that strategy, we’ll request computations from a backend Fibonacci server, using the HTTPClient object to do so. However, before we look at that, let’s first talk in general about using the HTTPClient object.

Node.js includes an HTTPClient object, which is useful for making HTTP requests. It has the capability to issue any kind of HTTP request. In this section, we’ll use the HTTPClient object to make HTTP requests similar to calling a REST web service.

Let’s start with some code inspired by the wget or curl commands to make HTTP requests and show the results. Create a file named wget.js, containing the following code:

const http = require(‘http’);

const url = require(‘url’);

const util = require(‘util’);

const argUrl = process.argv[2];

const parsedUrl = url.parse(argUrl, true);

// The options object is passed to http.request

// telling it the URL to retrieve const options = {

host: parsedUrl.hostname,

port: parsedUrl.port,

path: parsedUrl.pathname,

method: ‘GET’

};

if (parsedUrl.search) options.path += ‘?${parsedUrl.search}’;

const req = http.request(options);

// Invoked when the request is finished

req.on(‘response’, res => {

console.log(‘STATUS: ${res.statusCode}’);

console.log(‘HEADERS: ${util.inspect(res.headers)}’);

res.setEncoding(‘utf8’);

res.on(‘data’, chunk => { console.log(`BODY: ${chunk}`); });

res.on(‘error’, err => { console.log(`RESPONSE ERROR: ${err}’); });

});

// Invoked on errors

req.on(‘error’, err => { console.log(`REQUEST ERROR: ${err}’); });

req.end();

We invoke an HTTP request by using http.request, passing in an options object describing the request. In this case, we’re making a GET request to the server described in a URL we provide on the command line. When the response arrives, the response event is fired and we can print out the response. Likewise, an error event is fired on errors, and we can print out the error.

This corresponds to the HTTP protocol, where the client sends a request and receives a response.

You can run the script as follows:

$ node wget.js http://example.com STATUS: 200

HEADERS: {

‘accept-ranges’: ‘bytes’,

‘cache-control’: ‘max-age=604800’,

‘content-type’: ‘text/html; charset=UTF-8’,

date: ‘Mon, 06 Jan 2020 02:29:51 GMT’,

etag: ‘”3147526947″‘,

expires: ‘Mon, 13 Jan 2020 02:29:51 GMT’,

‘last-modified’: ‘Thu, 17 Oct 2019 07:18:26 GMT’,

server: ‘ECS (sjc/4E73)’,

vary: ‘Accept-Encoding’,

‘x-cache’: ‘HIT’,

‘content-length’: ‘1256’,

connection: ‘close’

}

BODY: <!doctype html>

<html>

 

Yes, example.com is a real website—visit it someday. There’s more in the printout, namely the HTML of the page at http://example.com/. What we’ve done is demonstrated how to invoke an HTTP request using the http.request function.

The options object is fairly straightforward, with the host, port, and path fields specifying the URL that is requested. The method field must be one of the HTTP verbs (GET, PUT, POST, and so on). You can also provide a headers array for the headers in the HTTP request. For example, you might need to provide a cookie:

var options = {

headers: { ‘Cookie’: ‘.. cookie value’ }

};

The response object is itself an EventEmitter object that emits the data and error events. The data event is called as data arrives and the error event is, of course, called on errors.

The request object is a WritableStream object, which is useful for HTTP requests containing data, such as PUT or POST. This means the request object has a write function, which writes data to the requester. The data format in an HTTP request is specified by the standard MIME type, which was originally created to give us a better email service. Around 1992, the World Wide Web (WWW) community worked with the MIME standard committee, who were developing a format for multi-part, multi- media-rich electronic mail. Receiving fancy-looking email is so commonplace today that you might not be aware that email used to come in plaintext. MIME types were developed to describe the format of each piece of data, and the WWW community adopted this for use on the web. HTML forms will post with a content type of multipart/form-data, for example.

The next step in offloading some computation to a backend service is to implement the REST service and to make HTTP client requests to that service.

Source: Herron David (2020), Node.js Web Development: Server-side web development made easy with Node 14 using practical examples, Packt Publishing.

Leave a Reply

Your email address will not be published. Required fields are marked *