-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrestful.js
31 lines (28 loc) · 830 Bytes
/
restful.js
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
27
28
29
30
31
/*
jsonplaceholder.typicode.com gives you error if you send frequet requests!
*/
const http = require('http');
const options = {
host: 'jsonplaceholder.typicode.com',
port: 80,
path: '/users',
method: 'GET'
};
const req = http.request(options, (res) => {
res.on('data', (data) => {
var jsonObj = JSON.parse(data); // convert from string to json object
console.log(jsonObj);
if (res.statusCode == 200) {
console.log(`There are ${jsonObj.length} users`);
for (let user of jsonObj) {
console.log(user.name);
}
} else {
console.log(`Request failed. HTTP response code = ${res.statusCode}`);
}
});
});
req.on('error', (error) => {
console.log(`Problem with request: ${error.message}`);
});
req.end();