-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
188 lines (168 loc) · 5.5 KB
/
app.js
File metadata and controls
188 lines (168 loc) · 5.5 KB
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
// This is the main code for the application. The modules below all help with networking and parsing the ejs templates for user data
//jshint esversion:6
const express = require("express");
const bodyParser = require("body-parser");
const mongoose = require("mongoose");
//Create application
const app = express();
let displayedArticles = [];
app.set("view engine", "ejs");
app.use(bodyParser.urlencoded({
extended: true
}));
//Use "public" files as home directory
app.use(express.static("public"));
//connect to database
mongoose.connect("mongodb+srv://admin-conork:Franklinglen16@cluster0-5je4y.mongodb.net/treeDB", {
useNewUrlParser: true
});
const articleSchema = {
name: String,
author: String,
date: String,
keywords: String
};
const Article = mongoose.model("ArticleEntry", articleSchema);
//print to console (used for testing code)
function print(articles, attr) {
articles.forEach(function(article) {
if (attr == "name") {
console.log(article.name);
}
if (attr == "author") {
console.log(article.author);
}
});
};
//These are the default articles
const article1 = new Article({
name: "Intra-annual nutrient flux in Pinus taeda",
author: "Timothy J. Albaugh, H. Lee Allen, Jose L. Stape, Thomas R. Fox, Rafael A. Rubilar and James W. Price",
date: "01/20/2012",
keywords: "Pinus taeda, flux"
});
const article2 = new Article({
name: "Ecosystem Nutrient Retention after Fertilization of Pinus taeda",
author: "Timothy J. Albaugh, L. Chris Kiser, Thomas R. Fox, H. Lee Allen, Rafael A. Rubilar, and Jose L. Stape",
date: "2014",
keywords: "fertilization, irrigation, nutrient balance, Pinus taeda"
});
const article3 = new Article({
name: "Soil Nitrogen Turnover is Altered by Herbicide Treatment in a North Carolina Piedmont Forest Soil",
author: "Steven W. Andariese, Peter M. Vitousek",
date: "04/14/1987",
keywords: "herbicide, soil nitrogen, insecticides"
});
const article4 = new Article({
name: "Irrigation and fertilization effects on foliar and soil carbon and nitrogen isotope ratios in a loblolly pine stand",
author: "Woo-Jung Choi, Scott X. Chang, H. Lee Allen, Daniel L. Kelting, Hee-Myong Ro",
date: "03/23/2005",
keywords: "irrigation, fertilization, loblolly, isotope"
});
const article5 = new Article({
name: "Characterization of Foliar Macro- and Micronutrient Concentrations and Ratios in Loblolly Pine Plantations in the Southeastern United States",
author: "Janine M. Albaugh, Leandra Blevins, H. Lee Allen, Timothy J. Albaugh, Thomas R. Fox, Jose L. Stape, and Rafael A. Rubilar",
date: "2010",
keywords: "Pinus taeda, foliar nutrient concentrations, nutrient ratios, loblolly"
});
const article6 = new Article({
name: "TestName",
author: "TestAuthor",
date: "01/31/2019",
keywords: "test, keyword"
});
let defaultArticles = [article1, article2, article3, article4, article5, article6];
//Have the applicatio listen to port 80
// app.listen(process.env.PORT || 3000, function() {
app.listen(80, function() {
console.log("Server is running on port 80");
});
//Handle get request
app.get("/", function(req, res) {
res.render("home");
});
app.get("/publish", function(req, res) {
res.render("publish");
});
//Search method called when user seartches for a specific article name
function search(foundArticles, searchedName) {
matchedArticles = [];
str = searchedName.replace(/\s/g, '');
if (str == "") {
return matchedArticles;
}
foundArticles.forEach(function(article) {
if (article.name.toLowerCase().includes(searchedName.toLowerCase())) {
matchedArticles.push(article);
}
else if (article.author.toLowerCase().includes(searchedName.toLowerCase())) {
matchedArticles.push(article);
}
else if (article.keywords.toLowerCase().includes(searchedName.toLowerCase())) {
matchedArticles.push(article);
}
});
return matchedArticles;
};
//Handle post requests
app.post("/", function(req, res) {
searchedName = req.body.searchName;
potentialArticles = [];
Article.find({}, function(err, foundArticles) {
if (foundArticles.length == 0) {
Article.insertMany(defaultArticles, function(err) {
console.log("No articles in database... Adding default papers...");
if (err) {
console.log(err);
}
else {
console.log("Successfully saved default articles to database!!!");
}
});
res.render("search-results", {
potentialArticles: defaultArticles
});
displayedArticles = defaultArticles;
}
else {
displayedArticles = search(foundArticles, searchedName);
if (searchedName == "") {
displayedArticles = foundArticles;
}
res.render("search-results", {
potentialArticles: displayedArticles
});
}
});
});
app.post("/publish", function(req, res) {
const newArticle = new Article({
name: req.body.newName,
author: req.body.newAuthor,
date: req.body.newDate,
keywords: req.body.newKeyWords
});
newArticle.save()
res.redirect("/");
});
//Remove articles when user deletes them
function remove(id, articles) {
for (var i = 0; i < articles.length; i++) {
if (articles[i]._id == id) {
articles.splice(i, 1);
}
}
return articles;
};
app.post("/delete", function(req, res) {
const id = req.body.deleteButton;
Article.findByIdAndDelete(id, function(err) {
if (err) {
console.log(err);
}
});
displayedArticles = remove(id, displayedArticles);
res.render("search-results", {
potentialArticles: displayedArticles
});
});