forked from zkirill/nodejs-mongo-angular-crud
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
258 lines (222 loc) · 6.21 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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
var http = require('http');
var url = require('url');
var mongodb = require('mongodb');
var ObjectID = mongodb.ObjectID;
var fs = require('fs');
/**
* Receive and parse POST request.
* @param req
* @param callback
*/
function handlePOST (req, callback) {
var body = '';
req.on('data', function (data) {
body += data;
// Prevent massive attack.
if (body.length > 1e6) {
req.connection.destroy();
callback();
}
});
req.on('end', function () {
var post;
try {
post = JSON.parse(body);
} catch (e) {
return callback(e);
}
return callback(null, post);
});
}
// Our MongoDB connection settings.
process.env.MONGODB_HOST = '127.0.0.1';
process.env.MONGODB_PORT = 27017;
process.env.MONGODB_DB = 'stuff';
// Our HTTP server settings.
process.env.IP = '0.0.0.0';
process.env.PORT = 8080;
// Connect to our MongoDB server.
var mongoServer = new mongodb.Server(process.env.MONGODB_HOST, process.env.MONGODB_PORT, {});
// Open our MongoDB database.
var db = new mongodb.Db(process.env.MONGODB_DB, mongoServer, {w: 0});
db.open(function (err) {
if (err) {
throw err;
}
});
// Store HTML in this file.
var html;
// Read HTML into memory.
fs.readFile(__dirname + '/app.html', 'UTF-8', function (err, data) {
if (err) {
throw err;
}
html = data;
});
// Create HTTP server.
http.createServer(function (req, res) {
// Extract URL parts.
var parts = url.parse(req.url, true);
// Extract query.
var query = parts.query;
// Extract path name.
var pathname = parts.pathname;
if (pathname.match(new RegExp('^/api/document'))) {
// Get document ID from query, if exists.
var id = query.id;
// Route based on whether document ID was passed.
if (id) {
// Convert string ID to MongoDB Object ID.
try {
id = new ObjectID(id);
} catch (e) {
res.writeHead(400);
return res.end();
}
if (req.method === 'GET') {
// Open our collection of documents.
return db.collection('documents', function (err, col) {
if (err) {
throw err;
}
// Compose document query.
var query = {
_id: id
};
// Find one document.
return col.findOne(query, function (err, doc) {
if (err) {
throw err;
}
var payload;
// Did we find our document?
if (doc) {
res.writeHead(200, {
'Content-Type': 'application/json'
});
payload = JSON.stringify(doc);
} else {
res.writeHead(404, {
'Content-Type': 'application/json'
});
payload = null;
}
return res.end(payload);
});
});
} else if (req.method === 'PUT') {
// Receive data from front end.
return handlePOST(req, function (err, data) {
if (err) {
throw err;
}
var document = {
date: data.date
};
var query = {
_id: id
};
// Upsert document.
return db.collection('documents', function (err, col) {
if (err) {
throw err;
}
return col.update(query, document, function (err, data) {
if (err) {
throw err;
}
res.writeHead(200, {
'Content-Type': 'application/json'
});
return res.end(JSON.stringify(data));
});
});
});
} else if (req.method === 'DELETE') {
// Open our documents collection.
return db.collection('documents', function (err, col) {
if (err) {
throw err;
}
// Compose query.
var query = {
_id: id
};
// Remove document that satisfied query, set "justOne" flag to true.
return col.remove(query, 1, function (err) {
if (err) {
throw err;
}
return res.end();
});
});
} else {
// Unsupported or invalid method.
res.writeHead(400);
return res.end();
}
} else { /* We didn't get passed a document ID. */
// Return all documents if document ID is not provided.
if (req.method === 'GET') {
// Open our collection of documents.
return db.collection('documents', function (err, col) {
if (err) {
throw err;
}
// Find all objects.
return col.find(query).toArray(function (err, docs) {
if (err) {
throw err;
}
var payload;
// Does the collection contain any documents?
if (docs) {
res.writeHead(200, {
'Content-Type': 'application/json'
});
payload = JSON.stringify(docs);
} else {
res.writeHead(404, {
'Content-Type': 'application/json'
});
payload = null;
}
return res.end(payload);
});
});
} else if (req.method === 'POST') { /* Save document to MongoDB. */
// Receive data from front end.
return handlePOST(req, function (err, data) {
if (err) {
throw err;
}
// Insert our new document.
return db.collection('documents', function (err, col) {
if (err) {
throw err;
}
return col.insert(data, function (err, data) {
if (err) {
throw err;
}
res.writeHead(200, {
'Content-Type': 'application/json'
});
return res.end(JSON.stringify(data[0]));
});
});
});
} else {
// Unsupported or invalid method.
res.writeHead(400);
return res.end();
}
}
} else {
// Answer all unrecognized calls with app.html.
res.writeHead(200, {
'Content-Type': 'text/html'
});
return res.end(html);
}
}).listen(process.env.PORT, process.env.IP);