-
Notifications
You must be signed in to change notification settings - Fork 0
/
init.js
70 lines (54 loc) · 1.46 KB
/
init.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
var http = require('http');
var express = require('express');
var fs = require('fs');
var app = express();
var server = http.createServer(app);
app.configure(function() {
app.use(express.static(__dirname + '/Content'));
app.use(express.bodyParser());
});
var returnFile = function (repsonse, fileName) {
fs.readFile(__dirname + '/Content/' + fileName, function(err, data) {
if (err) {
response.statusCode(500);
return;
}
response.write(data);
});
};
var addStatic = function(item) {
app.get('/', function (request, response) {
returnFile(response, item);
});
};
addStatic('index.html');
addStatic('site.js');
addStatic('site.css');
var todoItems = [];
var currentId = 0;
app.post('/add', function (request, response) {
var newTask = {
id: currentId++,
added: new Date(),
todo: request.body.todo,
done: false
};
var responseText = JSON.stringify(newTask);
todoItems.push(newTask);
response.send(responseText);
});
app.post('/delete', function(request, response) {
var idToDelete = request.body.id;
for (var i = todoItems.length; i--;) {
var item = todoItems[i];
if (item.id == idToDelete) {
item.done = true;
break;
}
}
response.send({ success: true });
});
app.get('/getAll', function(request, response) {
response.send(JSON.stringify(todoItems));
});
server.listen(8080);