-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
135 lines (111 loc) · 3.03 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
// Authors: Avia Weinstein (aweinste), Dylan Corwin (dcorwin), Gus Ireland (aireland)
var express = require("express");
var app = express();
var coll;
var mongo = require('mongodb');
var host = 'localhost';
var port = mongo.Connection.DEFAULT_PORT;
var nameList;
var optionsWithEnableWriteAccess = { w: 1 };
var dbName = 'testDb';
var client = new mongo.Db(
dbName,
new mongo.Server(host, port),
optionsWithEnableWriteAccess
);
function openDb(onOpen){
client.open(onDbReady);
function onDbReady(error){
if (error)
throw error;
client.collection('donations', onCollectionReady);
}
function onCollectionReady(error, collection){
if (error)
throw error;
onOpen(collection);
// remove everything from the collection.
/*collection.remove({}, function(error){
if (error)
throw error;
onOpen(collection);
});*/
}
}
function closeDb(){
client.close();
}
openDb(onDbOpen);
function onDbOpen(collection){
// set coll to be the collection so that we have access to it later
coll = collection;
}
function onDocumentsInserted(err){
if (err)
throw err;
console.log('documents inserted!');
// closeDb();
}
function insertDocuments(collection, docs, done){
if (docs.length === 0){
done(null);
return;
}
var docHead = docs.shift(); //shift removes first element from docs
collection.insert(docHead, function onInserted(err){
if (err){
done(err);
return;
}
insertDocuments(collection, docs, done);
});
}
//Ok, I am bad at closures, I want to find out how to return all without using a global. Because globals are bad
function createDBEntry(list){
var objectList = new Array();
for(var i = 0; i < list.length; i++){
name = list[i][1];
email = list[i][15];
tempObject = {'name': name, 'email': email, unique: true};
objectList.push(tempObject);
}
console.log(objectList);
insertDocuments(coll, objectList, onDocumentsInserted)
}
app.use(express.bodyParser());
app.post("/create", function(request, response) {
// openDb(onDbOpen);
var list = request.body.files;
createDBEntry(list);
response.send({
success: true
});
});
app.get("/create", function(request, response) {
coll.find({}).toArray(function(err, doc){
if (err)
throw error;
//console.log(doc);
response.send({
list: doc,
success: true
});
});
});
app.delete("/create/:id", function(request, response){
//var objID = ObjectId.createFromHexString(request.params.id);
var query = { name: request.params.id};
console.log(query);
coll.remove(query, function logResult(error, numberDocsRemoved){
if (error)
throw error;
console.log(numberDocsRemoved)
});
response.send({
success: true
});
});
app.use(express.static(__dirname + '/static/'));
app.listen(5555, function(){
console.log("Express server listening on port " + 5555);
});