-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhttp-client.js
More file actions
26 lines (22 loc) · 821 Bytes
/
http-client.js
File metadata and controls
26 lines (22 loc) · 821 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
/* Create a file named http-client.js.
Write a program that performs an HTTP GET request to a URL provided to you
as the first command-line argument. Write the String contents of each
"data" event from the response to a new line on the console (stdout).
*/
// the http module allows you to create a server and issue HTTP
// requests and responses.
const http = require('http');
const url = process.argv[2];
// call the get method to initiate a get request
// the arguments are the url and a callback function
http
.get(url, (res) => {
// setEncoding method will convert the "data" events into
// strings instead of Node Buffer objects
res.setEncoding('utf8');
res.on('data', (data) => {
console.log(data);
});
res.on('error', console.error);
})
.on('error', console.error);