-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
129 lines (103 loc) · 3.37 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
var bodyParser = require("body-parser"),
mongoose = require("mongoose"),
express = require("express"),
westernCourse = require("./models/western"),
brownCourse = require("./models/brown"),
app = express(),
seedDB = require("./seeds"),
url = require('url')
// APP CONFIG
// mongoose.connect("mongodb://localhost/CourseCache", { useNewUrlParser: true });
mongoose.connect("mongodb+srv://cisac:cemtorun@cluster0-4fjcs.mongodb.net/CourseCache?retryWrites=true&w=majority",
{ useNewUrlParser: true });
//var url = "mongodb://localhost/CourseCache";
app.set("view engine", "ejs");
app.use(bodyParser.urlencoded({extended: true}));
app.use(express.static("public"));
// seedDB();
var model = ""; // define as global variable so it can keep it while changing from different gets
// RESTFUL ROUTES
// MAIN PAGE
app.get("/", function(req, res){
res.render("school");
})
app.get("/about", function(req, res){
res.render("about");
})
app.get("/contact", function(req, res){
res.render("contact");
})
app.get("/:school", function(req, res){
var school = "western";
var url = req.url;
if (url.includes("brown/courses") || url.includes("university=brown")) {
school = "brown";
} else if (url.includes("western/courses") || url.includes("university=western")) {
school = "western";
};
model = eval(school+"Course");
model.aggregate([{ $match: { associations : "random" }}, { $sample: {size: 3}}], function(err, randoms) {
if(err){
console.log(err);
} else {
res.render("search", {school: school, randoms: randoms}); // represents index.ejs
}
});
});
// COURSE SEARCH PAGE
app.get("/:school/courses", function(req, res){
var search = req.query.search.toLowerCase();
var temp = search.split(" ");
var finalSearch = '"' + temp.join('" "') + '"'
var school = "western";
var url = req.url;
if (url.includes("brown/courses") || url.includes("university=brown")) {
school = "brown";
} else if (url.includes("western/courses") || url.includes("university=western")) {
school = "western";
};
model = eval(school+"Course");
if (search == "random"){
model.aggregate([{ $match: { associations : "random" }}, { $sample: {size: 200}}], function(err, randoms) {
if(err){
console.log(err);
} else {
res.render("index", {school: school, courses: randoms}); // represents index.ejs
}
});
} else {
model.find({ $text: { $search: finalSearch } }, function(err, courses) {
if(err){
console.log(err);
} else {
res.render("index", {courses: courses, school: school}); // represents index.ejs
}
});
};
});
// SHOW ROUTE
app.get("/:school/courses/:id", function(req, res){
var school = "western";
var url = req.url;
var id = req.params.id;
if (url.includes("brown/courses") || url.includes("university=brown")) {
school = "brown";
} else if (url.includes("western/courses") || url.includes("university=western")) {
school = "western";
};
var x = req.url.split("courses/");
id = x[x.length-1];
model = eval(school+"Course");
model.findById(id, function(err, selectCourse){
if (err) {
console.log(err);
} else {
res.render("show", {course: selectCourse, school: school});
}
});
});
const port = process.env.PORT || 8080;
const ip = process.env.IP || "127.0.0.1";
app.listen(port,function(){
console.log("Server has started .... at port "+ port+" ip: "+ip);
});