-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
75 lines (67 loc) · 2.04 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
const express = require('express');
const bodyParser = require('body-parser');
const compression = require('compression');
const fs = require('fs');
const http = require('http');
const socketIO = require('socket.io');
const cors = require('cors');
const whiteList = ['http://localhost:3000'];
const corsOptions = {
origin: function (origin, callback) {
if (whiteList.indexOf(origin) !== -1) {
callback(null, true);
} else {
callback(new Error('NOT ALLOWED'));
}
},
};
let pokemonList = require('./data/pokemonList.json');
const handleFetchList = (req, res) => {
const { currLength } = req.body;
try {
res.json(pokemonList.slice(currLength, currLength + 10));
} catch {
res.status(400).json('fetchList error');
}
};
const handleFetchPokemon = (req, res) => {
try {
res.json(require(`./data/pokemons/${req.body.name}.json`));
} catch {
res.status(400).json('fetchPokemon error');
}
};
const app = express();
const server = http.createServer(app);
const io = socketIO(server);
// app.use(cors(corsOptions));
app.use(bodyParser.json());
app.use(compression());
io.on('connection', (socket) => {
fs.watchFile('./data/pokemonList.json', () => {
let changed = [];
let newList = JSON.parse(fs.readFileSync('./data/pokemonList.json'));
for (let i = 0; i < newList.length; i++) {
if (JSON.stringify(pokemonList[i]) != JSON.stringify(newList[i]))
changed.push({ i, data: newList[i] });
}
io.sockets.emit('updateLIst', changed);
pokemonList = newList;
});
fs.readdir(
'./data/pokemons',
(err, files) =>
!err &&
files.map((fileName) =>
fs.watchFile(`./data/pokemons/${fileName}`, () => {
io.sockets.emit(
'pokemonInfo',
JSON.parse(fs.readFileSync(`./data/pokemons/${fileName}`))
);
})
)
);
});
app.post('/fetchlist', (req, res) => handleFetchList(req, res));
app.post('/fetchPokemon', (req, res) => handleFetchPokemon(req, res));
server.listen(3001, () => console.log(`app is running on port 3001`));