/
server.js
executable file
·45 lines (37 loc) · 1.09 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
const app = require('express')();
const fs = require('fs');
const bodyParser = require('body-parser');
const find = require(__dirname + '/utilities/find.js')();
app.use(bodyParser.urlencoded({ extended: false }));
var data = {};
fs.exists('/mnt/var/todo-ddl/data.json', function(exists) {
if(exists){
data = JSON.parse(fs.readFileSync('/mnt/var/todo-ddl/data.json'));
}else{
fs.mkdir('/mnt/var/todo-ddl/', ()=>{});
}
});
app.listen(13233 /*default port*/, () => console.log('todo-ddl listening on port 13233!'));
app.post('/', (req, res) => {
let query = req.body;
query.data = JSON.parse(query.data);
if(!query.action || !query.email || !query.data){
return;
}
if(query.action == 'push'){
if(data.hasOwnProperty(query.email)){
data[query.email] = find.merge(data[query.email], query.data);
}else{
data[query.email] = query.data;
}
fs.writeFile('/mnt/var/todo-ddl/data.json', JSON.stringify(data), ()=>{});
res.send('ok');
}
if(query.action == 'pull'){
if(data.hasOwnProperty(query.email)){
res.send(data[query.email]);
}else{
res.status(404).send('');
}
}
});