blob: 4318f447fddab3e332a5cfa4a5727f1dab2d6a83 (
plain)
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
|
/* eslint no-console: off */
const fs = require('fs');
const axios = require('axios');
async function writeToJSON() {
let times = 1;
if (fs.existsSync('cron/times.txt')) {
times = parseInt(fs.readFileSync('cron/times.txt', 'utf8'), 10);
}
fs.writeFile('cron/times.txt', parseInt(times + 1, 10), 'utf8', err => {
if (err) {
console.log('An error occured while writing JSON Object to File.');
return console.log(err);
}
return undefined;
});
const response = await axios.get(`https://reqres.in/api/users?page=${times}`);
let old = [];
if (fs.existsSync('cron/users.json')) {
old = JSON.parse(fs.readFileSync('cron/users.json', 'utf8'));
}
const json = old.concat(response.data.data);
fs.writeFile('cron/users.json', JSON.stringify(json), 'utf8', err => {
if (err) {
console.log('An error occured while writing JSON Object to File.');
return console.log(err);
}
return console.log('JSON file has been saved.');
});
}
writeToJSON();
|