Using Node as an HTTP client
The HTTP object doesn't just provide server capabilities, it also affords us with client functionality. We might want to use this functionality for a myriad of purposes: HTTP-based API's (such as a REST-based interface), website scraping for statistical processing or in the absence of an API, or the first step in automated UI testing. In this task, we're going to use http.get
with process
to fetch external web pages dynamically via the command line.
Getting ready
We are not creating a server. So in the name of convention, we should use a different name for our new file. Let's call it fetch.js
.
How to do it...
The http.request
method allows us to make requests of any kind (for example, GET
, POST
, DELETE
, OPTION
, and so on), but for GET
requests, we can use the shorthand http.get
method as shown in the following code:
var http = require('http'); var urlOpts = {host: 'www.nodejs.org', path: '/', port: '80'}; http.get(urlOpts, function (response) { response.on('data...