-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathserver.js
53 lines (45 loc) · 1.17 KB
/
server.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
const express = require('express');
const fs = require('fs');
const bodyParser = require('body-parser')
const crypto = require('crypto')
const app = express();
const port=3000;
app.use(express.static('public'));
app.use(bodyParser.json({
limit: '1mb'
}));
app.post("/state/:id", (req,res)=>{
const hashid = hash(req.params.id);
fs.writeFileSync(`data/${hashid}`, JSON.stringify(req.body));
res.sendStatus(200);
});
app.get("/state/:id", (req,res)=>{
const hashid = hash(req.params.id);
try {
res.send(JSON.parse(fs.readFileSync(`data/${hashid}`)));
}
catch (e) {
console.log(e);
if(e.code==='ENOENT') {
res.sendStatus(404);
} else {
res.sendStatus(500);
}
}
});
app.listen(port, ()=>console.log(`listening to ${port}`));
function shuffle(a) {
for (let i = a.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[a[i], a[j]] = [a[j], a[i]];
}
return a;
}
function clone(val) {
return Object.assign({}, val);
}
function hash(value) {
const hash = crypto.createHash('md5')
hash.update(value);
return hash.digest('hex');
}