This repository has been archived by the owner on Mar 5, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
121 lines (105 loc) · 3.84 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
// Dependencies.
const
clients = require('./controller/apiClients'),
express = require('express'),
exphps = require('express-handlebars'),
path = require('path'),
bodyParser = require('body-parser'),
mongoose = require('mongoose'),
mongojs = require('mongojs');
require('dotenv').config();
// If deployed, use the deployed database.
// Otherwise use the local BYGStaff database.
const MONGODB_URI = process.env.MONGODB_URI || "mongodb://localhost/BYGStaff";
// Set mongoose to leverage built in JavaScript ES6 Promises.
// Connect to the Mongo DB.
mongoose.Promise = Promise;
mongoose.connect(MONGODB_URI, { useNewUrlParser: true });
// Hook models into the db variable.
var db = require("./models");
// Initialize Express.
const app = express();
// Adding in Handlebars.
app.engine('handlebars',exphps({defaultLayout:'main'}));
app.set('view engine','handlebars');
// Setting the base path to be used.
app.use(express.static(path.join(__dirname, 'public')));
app.use(bodyParser.urlencoded({ extended: true }));
clients(app);
// Setting a base port to be used.
const PORT = process.env.PORT || 9001;
// ------------------------POST AND GET REQUESTS---------------------------
// Retrieve data from the db to be used to populate the home page
app.get('/directory', function(request, response) {
// Find all results from the scrapedData collection in the db
db.Student.find({}).sort({grade: 1, name: 1})
.then(function(dbStudent){
console.log(dbStudent);
response.json(dbStudent);
})
.catch(function(error){
response.json(error);
});
});
// Retrieve data from the db to be used when a certain grade is selected
app.get('/directory/:grade', function(request, response) {
// Find all results from the scrapedData collection in the db
db.Student.find({grade: request.params.grade}).sort({grade: 1, name: 1})
.then(function(dbStudent){
console.log(dbStudent);
response.json(dbStudent);
})
.catch(function(error){
response.json(error);
});
});
// Retrieve data from the db to be used when a certain month is selected
app.get('/birthday/:month', function(request, response) {
// Find all results from the scrapedData collection in the db
db.Student.find({birthday: new RegExp(request.params.month + '+')}).sort({birthday: 1, grade: 1, name: 1})
.then(function(dbStudent){
console.log(dbStudent);
response.json(dbStudent);
})
.catch(function(error){
response.json(error);
});
});
// When the user clicks the Add Student button,
// Add the student onto the database
app.post('/all/students', function(request, response){
db.Student.create(request.body)
.then(function(dbStudent){
console.log("SERVER.JS DBSTUDENT: " + dbStudent);
})
.catch(function(error){
response.json(error);
});
});
// When the user clicks the Edit Student button
// Add the student information into the modal
app.get('/edit/students/:id', function(request, response){
db.Student.find({ _id : request.params.id })
.then(function(dbStudent){
console.log(dbStudent);
})
.catch(function(error){
response.json(error);
})
});
// When the user clicks the Delete Student button
// Delete the student from the database
app.put('/delete/students/:id', function(request, response){
db.Student.deleteOne({ _id : request.params.id })
.then(function(dbStudent){
console.log(dbStudent);
})
.catch(function(error){
response.json(error);
})
});
// ------------------------------------------------------------------------
// Setting up a base port to be used.
app.listen(PORT, ()=>{
console.log("Server started at PORT: " + PORT);
});