-
Notifications
You must be signed in to change notification settings - Fork 108
/
Copy pathproject2_server.js
142 lines (116 loc) · 4.08 KB
/
project2_server.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
/**
* tips:
* add more description inside the function
*/
const port = 8171;
const mongo_connection = {
'user': 'XX',
'password': 'XXX',
'db': 'XXX',
'mongo_host':'127.0.0.1',
// 'mongo_host': 'localhost',
'mongo_port': '27017',
'mongo_collection': 'university'
}
const express = require('express')
const app = express()
var allowCrossDomain = function (req, res, next) {
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE');
res.header('Access-Control-Allow-Headers', 'Content-Type');
next();
};
app.use(express.bodyParser());
app.use(allowCrossDomain);
app.use('/scripts', express.static(__dirname + '/scripts'));
app.use('/css', express.static(__dirname + '/css'));
app.use(express.static(__dirname));
// create mongodb connection
var mongo = require('mongodb');
var MongoClient = mongo.MongoClient;
//var mongo_string='mongodb://'+mongo_connection['user']+':'+mongo_connection['password']+'@'+mongo_connection['mongo_host']+':'+mongo_connection['mongo_port']
var mongo_string = 'mongodb://' + mongo_connection['mongo_host'] + ':' + mongo_connection['mongo_port']
mongo_string="mongodb://XX:XXXXXX@127.0.0.1:27017"
console.log(mongo_string)
const client = new MongoClient(mongo_string)
// a tricky way to save collection to global variable
var u_collection;
var a;
// TODO know why connection string cannot work but must hard-coded
MongoClient.connect("mongodb://h_wang:A00431268@127.0.0.1:27017/h_wang", function (err, db) {
if (err)
console.log(err);
else {
console.log('Mongo Conn....');
this.u_collection = db.collection("university")
this.a = 1
}
});
app.get('/', (req, res) => res.send('Hello World!'))
var server = app.listen(port, () => console.log(`Example app listening on port ${port}!`))
app.post('/saveUniversity', function (request, response) {
console.log(request.body);
console.log("a = " + this.a);
/**
* parse all data .
* 1. Check whether it exists : https://stackoverflow.com/questions/8389811/how-to-query-mongodb-to-test-if-an-item-exists
* 2. if not , insert one to collection
*/
this.u_collection.count(request.body, function (err, count) {
if (count >= 1) {
response.send(200, "Records Already Exixts")
} else {
this.u_collection.insertOne(request.body, function (err, res) {
if (err) throw err;
console.log("1 document inserted");
response.send(200, "Save University Successfully")
})
}
})
})
app.post('/deleteUniversity', function (request, response) {
console.log(request.body);
this.u_collection.count(request.body, function (err, count) {
if (count < 1) {
response.send(200, "Records doesn't Exixts")
} else {
this.u_collection.remove(request.body, function (err, res) {
if (err) throw err;
console.log("1 document deleted");
response.send(200, "Delete University Successfully")
})
}
})
})
app.post('/searchUniversity', function (request, response) {
this.u_collection.count({
"Name": request.body.Name
}, function (err, count) {
if (count < 1) {
response.send(400, "Records don't Exixts")
} else {
console.log(request.body.Name)
this.u_collection.find({
"Name": request.body.Name
}).toArray(function (err, res) {
console.log("result is " + res)
response.send(200,res)
})
}
})
})
app.post('/displayUniversity', function (request, response) {
this.u_collection.count({
}, function (err, count) {
if (count < 1) {
response.send(400, "Records don't Exixts")
} else {
console.log(request.body.Name)
this.u_collection.find({
}).toArray(function (err, res) {
console.log("result is " + res)
response.send(200,res)
})
}
})
})