-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
executable file
·62 lines (53 loc) · 1.48 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
54
55
56
57
58
59
60
61
var express = require('express'),
swig = require('swig'),
cons = require('consolidate'),
fs = require('fs');
bodyParser = require('body-parser');
uuid = require('node-uuid');
var app = express(),
env = "prod",
bd = './base-data.json',
baseData = fs.readFileSync(bd).toString(),
server = require('http').createServer(app),
data = JSON.parse(baseData);
var home = function (req, res) {
res.render('index',{
posts : data,
env : env
});
};
app.engine('.html', cons.swig);
app.set('view engine', 'html');
app.set('views', './app/views');
app.use(bodyParser());
app.use( express.static('./public') );
app.get('/tasks/', function(req, res){
res.send(data);
});
app.get('/', home);
app.post('/tasks', function (req, res){
req.body.id = uuid.v1();
data.push(req.body);
fs.writeFile(bd, JSON.stringify(data));
res.send(200, {status:"Ok", id: req.body.id});
});
app.put('/tasks/:id', function (req, res){
for (var task in data){
if (data[task].id === req.params.id)
data[task].status = data[task].status != req.body.status ? req.body.status : data[task].status;
}
fs.writeFile(bd, JSON.stringify(data));
res.send(200, {status:"Ok", id: req.params.id});
});
app.delete('/tasks/:id', function (req, res){
for (var task in data){
if (data[task].id === req.params.id){
delete data[task];
}
}
var dat = JSON.stringify(data).replace(",null", "");
data = JSON.parse(dat);
fs.writeFile(bd, dat);
res.send(200, {status:"Ok", id: req.params.id});
});
app.listen(3000);