-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
61 lines (48 loc) · 1.5 KB
/
index.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
const express = require('express');
const requestLimit = require('express-rate-limit');
const fetch = require('node-fetch');
const fs = require('fs-extra');
const path = require('path');
const util = require('util');
const streamPipeline = util.promisify(require('stream').pipeline);
const app = express();
const port = 3000;
app.use(express.static('public'));
app.use(express.static('imagecache'));
const limiter = requestLimit({
windowMs: 1000,
max: 100,
onLimitReached: (req, res, options) => {
console.log('LIMITE!!!');
},
});
app.use(limiter);
app.listen(port, () => {
console.log(`Escutando em ${port}`);
});
app.get('/fetchJSON/*', async (req, res) => {
console.log('Pegando JSON: ' + req.params[0]);
const response = await fetch(req.params[0], {
'Content-Type': 'application/json',
'Accept': 'application/json',
});
const json = await response.json();
res.send(json);
});
app.get('/fetchImage/*', async (req, res) => {
const filename = `imagecache/${req.params[0].split('/').reverse()[0]}`;
if (!fs.existsSync(filename)) {
console.log('Baixando IMAGEM: ' + req.params[0]);
const response = await fetch(req.params[0]);
if (response.ok) {
await streamPipeline(response.body, fs.createWriteStream(filename));
}
}
console.log('Enviando IMAGEM: ' + filename);
res.sendFile(filename, {root: __dirname});
});
app.get('/reset', async (req, res) => {
const dir = path.resolve('./imagecache/');
console.log('DELETANDO ' + dir);
fs.emptyDir(dir);
});