-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
55 lines (40 loc) · 1.14 KB
/
server.js
File metadata and controls
55 lines (40 loc) · 1.14 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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
const express = require('express');
const fs = require('fs');
const { routes } = require('./routes');
const port = process.env.PORT || 3000;
const app = express();
const returnCache = (location, res) => {
const path = './cache'+location;
console.log(path+'.json')
if (fs.existsSync(path)) {
const stats = fs.statSync(path);
if (stats.isDirectory()) {
fs.readFile(path+'/_all.json', 'utf8', (err, data) => {
if (err) {
return;
}
const response = JSON.parse(data);
res.status(200).json(response)
})
} else {
fs.readFile(path+'.json', 'utf8', (err, data) => {
if (err) {
return;
}
const response = JSON.parse(data);
res.status(200).json(response)
})
}
} else {
console.log('The file or directory does not exist');
res.status(200).json({});
}
}
app.route('*')
.get((req, res) => {
console.log(req.originalUrl)
returnCache(req.originalUrl, res)
})
app.listen(port, () => {
console.log(`Server started on port ${port}`);
});