forked from ajaugust44/we-code-team-10
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
executable file
·204 lines (175 loc) · 6.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
var express = require("express"); // imports express
var app = express(); // create a new instance of express
// imports the fs module (reading and writing to a text file)
var fs = require("fs");
// the bodyParser middleware allows us to parse the
// body of a request
// app.use(express.bodyParser());
var bodyParser = require('body-parser');
app.use(bodyParser.urlencoded({ extended: true }));
// The global datastore for this example
var listings;
// Asynchronously read file contents, then call callbackFn
function readFile(filename, defaultData, callbackFn) {
fs.readFile(filename, function(err, data) {
if (err) {
console.log("Error reading file: ", filename);
data = defaultData;
} else {
console.log("Success reading file: ", filename);
}
if (callbackFn) callbackFn(err, data);
});
}
// Asynchronously write file contents, then call callbackFn
function writeFile(filename, data, callbackFn) {
fs.writeFile(filename, data, function(err) {
if (err) {
console.log("Error writing file: ", filename);
} else {
console.log("Success writing file: ", filename);
}
if (callbackFn) callbackFn(err);
});
}
// get all items
app.get("/listings", function(request, response){
response.send({
listings: listings,
success: true
});
});
// get one item
app.get("/listings/:id", function(request, response){
var id = request.params.id;
var item = listings[id];
response.send({
listings: item,
success: (item !== undefined)
});
});
// create new item
app.post("/listings", function(request, response) {
console.log(request.body);
var item = {"desc": request.body.desc,
"author": request.body.author,
"date": new Date(),
"price": Number(request.body.price),
"sold": false };
var successful =
(item.desc !== undefined) &&
(item.author !== undefined) &&
(item.price !== undefined);
if (successful) {
listings.push(item);
writeFile("data.txt", JSON.stringify(listings));
} else {
item = undefined;
}
response.send({
item: item,
success: successful
});
});
// update one item
app.put("/listings/:id", function(request, response){
// change listing at index, to the new listing
var id = request.params.id;
var oldItem = listings[id];
var item = { "desc": request.body.desc,
"author": request.body.author,
"date": new Date(),
"price": request.body.price,
"sold": request.body.sold };
item.desc = (item.desc !== undefined) ? item.desc : oldItem.desc;
item.author = (item.author !== undefined) ? item.author : oldItem.author;
item.price = (item.price !== undefined) ? item.price : oldItem.price;
item.sold = (item.sold !== undefined) ? JSON.parse(item.sold) : oldItem.sold;
// commit the update
listings[id] = item;
writeFile("data.txt", JSON.stringify(listings));
response.send({
item: item,
success: true
});
});
// delete entire list
app.delete("/listings", function(request, response){
listings = [];
writeFile("data.txt", JSON.stringify(listings));
response.send({
listings: listings,
success: true
});
});
// delete one item
app.delete("/listings/:id", function(request, response){
var id = request.params.id;
var old = listings[id];
listings.splice(id, 1);
writeFile("data.txt", JSON.stringify(listings));
response.send({
listings: old,
success: (old !== undefined)
});
});
// This is for serving files in the static directory
app.get("/static/:staticFilename", function (request, response) {
response.sendfile("static/" + request.params.staticFilename);
});
// This is for serving files in the static directory
app.get("/static/media_button_bar/css/:staticFilename", function (request, response) {
response.sendfile("static/media_button_bar/css/" + request.params.staticFilename);
});
// This is for serving files in the static directory
app.get("/static/app_framework/2.1/css/:staticFilename", function (request, response) {
response.sendfile("static/app_Framework/2.1/css/" + request.params.staticFilename);
});
// This is for serving files in the static directory
app.get("/static/css/:staticFilename", function (request, response) {
response.sendfile("static/css/" + request.params.staticFilename);
});
// This is for serving files in the static directory
app.get("/static/js/:staticFilename", function (request, response) {
response.sendfile("static/js/" + request.params.staticFilename);
});
// This is for serving files in the static directory
app.get("/static/xdk/:staticFilename", function (request, response) {
response.sendfile("static/xdk/" + request.params.staticFilename);
});
// This is for serving files in the static directory
app.get("/static/xdk/ad/:staticFilename", function (request, response) {
response.sendfile("static/xdk/ad/" + request.params.staticFilename);
});
// This is for serving files in the static directory
app.get("/static/app_Framework/2.1/:staticFilename", function (request, response) {
response.sendfile("static/app_Framework/2.1/" + request.params.staticFilename);
});
// This is for serving files in the static directory
app.get("/static/lib/:staticFilename", function (request, response) {
response.sendfile("static/lib/" + request.params.staticFilename);
});
// This is for serving files in the static directory
app.get("/static/sidebar/js/:staticFilename", function (request, response) {
response.sendfile("static/sidebar/js/" + request.params.staticFilename);
});
// This is for serving files in the static directory
app.get("/static/js/:staticFilename", function (request, response) {
response.sendfile("static/js/" + request.params.staticFilename);
});
// This is for serving files in the static directory
app.get("/static/images/:staticFilename", function (request, response) {
response.sendfile("static/images/" + request.params.staticFilename);
});
//app.use(express.static(__dirname + '/static'));
function initServer() {
// When we start the server, we must load the stored data
var defaultList = "[]";
readFile("data.txt", defaultList, function(err, data) {
listings = JSON.parse(data);
});
}
// Finally, initialize the server on any port, and go to that url
// in your browser
initServer();
app.listen(8889);