-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
92 lines (82 loc) · 2.7 KB
/
app.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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
const express = require('express')
const request = require('request')
const cronJob = require('cron').CronJob;
const fs = require('fs')
const app = express();
const log = console.log;
const downloadAndSave = function(uri, name, cb) {
return request.head(uri, function(err, res, body) {
return request(uri).pipe(fs.createWriteStream(name)).on('close', cb);
})
};
const convertToBase64 = function (file) {
var bitmap = fs.readFileSync(file);
return new Buffer(bitmap).toString('base64');
};
app.get('/', (req, res) => {
res.send('Hello payever nodejs task!')
});
//get user by id
app.get('/user/:userId', (req, res) => {
const userId = req.params.userId;
//make the request to get users
request('https://reqres.in/api/users/'+userId, {json: true}, (err, result, body) => {
if (err) { return log(err); }
log(body);
return res.send(body.data);
});
});
//get avatar by userid
app.get('/user/:userId/avatar', (req, res) => {
const userId = req.params.userId;
//check if the file already exists in our system
const filename = './'+userId+'.jpg';
if (fs.existsSync(filename)) {
//convert it into base64 and send to the user
return res.send(convertToBase64(filename));
}
//else make the call to get the image
request('https://reqres.in/api/users/'+userId, {json: true}, (err, result, body) => {
if (err) { return log(err); }
log(body.data.avatar);
const uri = body.data.avatar;
let isSaved = downloadAndSave(uri, userId+'.jpg', function() {
log('downloaded and saved into the file system');
return res.send(convertToBase64(filename));
});
});
});
//delete avatar by userid
app.delete('/user/:userId/avatar', (req, res) => {
const userId = req.params.userId;
return fs.unlink(userId+'.jpg', function() {
return res.send('image deleted...');
});
});
//activating a cronjob for every one minute
let i = 0;
new cronJob('* * * * * *', function() {
request('https://reqres.in/api/users?page='+i, {json: true}, (err, result, body) => {
if (err) { return log(err); }
if (!fs.existsSync('users.json')) {
fs.writeFile("users.json", JSON.stringify(body.data), function(err){
if (err) throw err;
console.log('The data has been added...');
});
} else {
//need to append data
fs.readFile('users.json', function (err, data) {
var json = JSON.parse(data); //existing data
json.push(...body.data);
fs.writeFile("users.json", JSON.stringify(json), function (err) {
if (err) throw err;
console.log('data appended..');
});
});
}
i++;
});
}, null, true, 'America/Los_Angeles');
app.listen(3000, () => {
console.log('Example app listening on port 3000!')
});