-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
41 lines (36 loc) · 1.13 KB
/
Copy pathindex.js
File metadata and controls
41 lines (36 loc) · 1.13 KB
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
32
33
34
35
36
37
38
39
40
41
const https = require("https");
const fs = require("fs");
https
.get("https://jsonplaceholder.typicode.com/users", (res) => {
let data = [];
const headerDate =
res.headers && res.headers.date ? res.headers.date : "no response date";
console.log("Status Code:", res.statusCode);
console.log("Date in Response header:", headerDate);
res.on("data", (chunk) => {
data.push(chunk);
});
res.on("end", () => {
console.log("Response ended: ");
const users = JSON.parse(Buffer.concat(data).toString());
const headerColumn = Object.keys(users[0]).toString();
let dataString = "";
dataString += headerColumn + "\n";
users.map((user) => {
dataString +=
Object.keys(user)
.map((key) => {
if (typeof user[key] === "object") {
return JSON.stringify(user[key]);
}
return user[key];
})
.toString() + "\n";
});
fs.writeFileSync("result.csv", dataString);
console.log(users);
});
})
.on("error", (err) => {
console.log("Error: ", err.message);
});