-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
177 lines (152 loc) · 4.51 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
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
// Dependencies
var express = require("express");
var bodyParser = require("body-parser");
var mongoose = require("mongoose");
var path = require("path");
var methodOverride = require("method-override");
// Requiring our Note and Article models
var Note = require("./models/note.js");
var Article = require("./models/article.js");
// Our scraping tools
var request = require("request");
var cheerio = require("cheerio");
// Set mongoose to leverage built in JavaScript ES6 Promises
mongoose.Promise = Promise;
// Initialize Express
var app = express();
var PORT = process.env.PORT || 3000;
// Use body parser with our app
app.use(bodyParser.urlencoded({
extended: false
}));
// override with POST having ?_method=PUT
app.use(methodOverride('_method'));
// Make public a static dir
app.use(express.static("./public"));
// Set Handlebars.
var exphbs = require("express-handlebars");
app.set('views', __dirname + '/views');
app.engine("handlebars", exphbs({ defaultLayout: "main", layoutsDir: __dirname + "/views/layouts" }));
app.set("view engine", "handlebars");
// Database configuration with mongoose
var databaseUri = "mongodb://localhost/nhlscrape";
if (process.env.MONGODB_URI) {
mongoose.connect(process.env.MONGODB_URI);
} else {
mongoose.connect(databaseUri);
}
var db = mongoose.connection;
/*mongoose.connect("mongodb://heroku_dnbl2f3n:etrnrrvnoe7m1qmtae0jret6ss@ds111882.mlab.com:11882/heroku_dnbl2f3n");
var db = mongoose.connection;*/
// Show any mongoose errors
db.on("error", function(error) {
console.log("Mongoose Error: ", error);
});
// Once logged in to the db through mongoose, log a success message
db.once("open", function() {
console.log("Mongoose connection successful.");
});
//=========Routes==========//
app.get("/", function (req, res) {
Article.find({})
.exec(function (error, data) {
if (error) {
res.send(error);
}
else {
var newsObj = {
Article: data
};
res.render("index", newsObj);
}
});
});
// A GET request to scrape the nhl/oilers website
app.get("/scrape", function(req, res) {
// First, we grab the body of the html with request
request("https://www.nhl.com/oilers/", function(error, response, html) {
// Then, we load that into cheerio and save it to $ for a shorthand selector
var $ = cheerio.load(html);
// Now, we grab every h2 within an article tag, and do the following:
$("h4.headline-link").each(function(i, element) {
// Save an empty result object
var result = {};
// Add the text and href of every link, and save them as properties of the result object
result.title = $(this).text();
result.link = $(this).parent("a").attr("href");
// Using our Article model, create a new entry
// This effectively passes the result object to the entry (and the title and link)
var entry = new Article(result);
// Now, save that entry to the db
entry.save(function(err, doc) {
// Log any errors
if (err) {
console.log(err);
}
// Or log the doc
else {
console.log(doc);
}
});
});
res.redirect("/");
console.log("Successfully Scraped");
});
});
app.post("/notes/:id", function (req, res) {
var newNote = new Note(req.body);
newNote.save(function (error, doc) {
if (error) {
console.log(error);
}
else {
console.log("this is the DOC " + doc);
Article.findOneAndUpdate({
"_id": req.params.id
},
{ $push: { "note": doc._id } }, {new: true}, function (err, doc) {
if (err) {
console.log(err);
} else {
console.log("note saved: " + doc);
res.redirect("/notes/" + req.params.id);
}
});
}
});
});
app.get("/notes/:id", function (req, res) {
console.log("This is the req.params: " + req.params.id);
Article.find({
"_id": req.params.id
}).populate("note")
.exec(function (error, doc) {
if (error) {
console.log(error);
}
else {
var notesObj = {
Article: doc
};
console.log(notesObj);
res.render("notes", notesObj);
}
});
});
app.get("/delete/:id", function (req, res) {
Note.remove({
"_id":req.params.id
}).exec(function (error, doc) {
if (error) {
console.log(error);
}
else {
console.log("note deleted");
res.redirect("/" );
}
});
});
// Listen on port 3000
app.listen(PORT, function() {
console.log("App running on PORT" + PORT + "!");
});