forked from StevenAb/BEARS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Server.js
130 lines (108 loc) · 3.35 KB
/
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
const express = require('express');
const bodyParser = require('body-parser');
var db = require('mongodb');
const MongoClient = require('mongodb').MongoClient;
const app = express();
//actually retrieve data inputted
app.set('view engine', 'ejs');
app.use(express.static('c:\\Users\\almei\\Desktop\\Node\\TestServer' + '/views'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: true}));
MongoClient.connect('mongodb://stephanie_almeida:[email protected]:63377/bears-senior-project', (err, database) => {
if (err)
return console.log(err);
app.locals.db = database;
app.locals.id = 1;
//listening to certain port
app.listen(3000, function() {
console.log('listening on 3000')
});
});
//callback function --> what to do when path is matched
app.get('/', (req, res) => {
name = String(app.locals.id);
//res.sendFile('c:\\Users\\almei\\Desktop\\Node\\TestServer\\index.html');
app.locals.db.collection(name).find().toArray((err, result) => {
if (err)
return console.log(err);
res.render('index.ejs', {id: result});
});
});
app.post('/addusers', (req, res) => {
name = String(app.locals.id);
/*app.locals.db.collection(name).save(req.body, (err, result) => {
if (err)
return console.log(err);
console.log('saved to database');
app.locals.id = app.locals.id + 1;
res.redirect('/');
});*/
/* db.collection('usercollection').save(req.body, (err, result) => {
if (err)
return console.log(err);
console.log('saved to database');
id++;
res.redirect('/');
});*/
app.locals.db.collection("usercollection").insert([{user: req.body.user, pass: req.body.pass, id: app.locals.id}], (err, result) => {
if (err)
return console.log(err);
app.locals.id = app.locals.id + 1;
res.redirect('/');
app.locals.collection = app.locals.db.collection(name).save({}, (err, result) => {
if (err)
return console.log(err);
});
});
console.log(req.body);
});
app.post('/checkusers', (req, res) => {
var datab = app.locals.db;
app.locals.username = req.body.user;
app.locals.password = req.body.pass;
var collection = app.locals.db.collection("usercollection");
//console.log(collection);
collection.findOne({user: app.locals.username, pass: app.locals.password}, function(err, user) {
if (user == null) {
console.log("no username/pass");
res.redirect('back');
}
else if (err) {
return console.log(err);
}
else {
console.log(user.id);
app.locals.id = parseInt(user.id);
return res.redirect('/');
}
});
/*datab.getCollectionNames().forEach(function(collname) {
var user = db[collname].find({ user: {$gt: username}});
var pass = db[collname].find({ user: {$get: password}});
if (user == null && pass == null) {
alert('Either username or password is incorrect!');
}
else {
id = collname;
}
});*/
});
app.post('/delete', (req, res) => {
name = String(app.locals.id);
app.locals.db.collection(name).remove();
return res.redirect('/');
});
app.post('/addtranscript', (req, res) => {
console.log(req.body);
name = String(req.body.id);
if (req.body.transcript == "false") {
}
else {
app.locals.db.collection(name).save(req.body, (err, result) => {
if (err)
return console.log(err);
console.log('saved to db');
res.redirect('/');
});
}
});