-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
166 lines (132 loc) · 3.44 KB
/
app.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
var http = require('http');
var url = require('url');
var fs = require('fs');
/* Reads the file at the given path and serves it to the client. The
* Content-Type header will be set to the provided contentType. */
function readAndServe(path, contentType, response)
{
fs.readFile(path, function(error, data) {
if (error) {
throw error;
}
response.writeHead(200, {'Content-type': contentType});
response.write(data);
response.end();
});
}
/* Creates a task with the given text. Calls the callback when finished. */
function createTask(text, callback)
{
readTasks(function(tasks)
{
tasks.push({ text: text });
writeTasks(tasks, callback);
});
}
/* Reads all tasks from the filesystem. Calls the given callback, passing in an
* array of tasks, when finished. */
function readTasks(callback)
{
fs.readFile('tasks', function(error, contents)
{
if (error)
{
throw error;
}
var tasks;
if (contents.length === 0)
{
tasks = [];
}
else
{
tasks = JSON.parse(contents);
}
callback(tasks);
});
}
/* Writes the given tasks to the filesystem. Calls the given callback when
* finished. */
function writeTasks(tasks, callback)
{
var tasksJSON = JSON.stringify(tasks);
fs.writeFile('tasks', tasksJSON, function(error) {
if (error)
{
throw error;
}
callback();
});
}
function deletetask(id,callback)
{
readTasks(function(tasks)
{
tasks.splice(id-1, 1);
writeTasks(tasks, callback)
});
}
/* Reads the JSON body of the request and parses it. Calls the given callback,
* passing in the parsed object. */
function readJSONBody(request, callback)
{
var body = '';
request.on('data', function(chunk) {
body += chunk;
});
request.on('end', function() {
var data = JSON.parse(body);
callback(data);
});
}
/* Serves files for the task list, and provides routes to create/delete tasks. */
http.createServer(function(request, response)
{
var pathname = url.parse(request.url).pathname;
if (request.method === "GET") {
if (pathname === "/") {
readAndServe('index.html', 'text/html', response);
} else if (pathname === "/css/style.css" ||
pathname === "/css/normalize.css") {
readAndServe('.' + pathname, 'text/css', response);
} else if (pathname === "/js/script.js" ||
pathname === "/js/handlebars.js") {
readAndServe('.' + pathname, 'text/javascript', response);
} else if (pathname === "/images/pattern.png") {
readAndServe('.' + pathname, 'image/png', response);
} else if (pathname === "/tasks") {
readTasks(function(tasks) {
response.writeHead(200, {'Content-type': 'application/json'});
response.write(JSON.stringify(tasks));
response.end();
});
} else {
response.end();
}
} else if (request.method === "POST") {
if (pathname === "/tasks") {
readJSONBody(request, function(task) {
createTask(task.text, function() {
// must wait until task is stored before returning response
response.end();
});
});
}
else if(pathname === "/deletetask")
{
readJSONBody(request, function(task) {
console.log(task);
deletetask(task, function()
{
response.end();
});
});
}
else {
response.end();
}
} else {
response.end();
}
}).listen(8000, '127.0.0.1');
console.log('Running on 127.0.0.1:8000');