-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
195 lines (147 loc) · 5.47 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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
var express = require("express");
var bodyParser = require("body-parser");
var mongoose = require("mongoose");
var cheerio = require("cheerio");
var exphbs = require("express-handlebars");
var request = require('request');
// Require all models
var db = require("./models");
var PORT = process.env.PORT || 3000;
var app = express();
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
app.use(express.static("public"));
app.engine("handlebars", exphbs({ defaultLayout: "main" }));
app.set("view engine", "handlebars");
// If deployed, use the deployed database. Otherwise use the local mongoHeadlines database
var MONGODB_URI = process.env.MONGODB_URI || "mongodb://localhost/mongoHeadlines";
// Set mongoose to leverage built in JavaScript ES6 Promises
// Connect to the Mongo DB
mongoose.Promise = Promise;
mongoose.connect(MONGODB_URI);
var links = [];
var titles = [];
// Routes
app.get("/", function (req, res) {
// First, we grab the body of the html with request
request("https://www.nytimes.com/", 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:
$("h2.story-heading").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
var title = result.title = $(this)
.children("a")
.text();
var link = result.link = $(this)
.children("a")
.attr("href");
var summary = result.summary = $(this)
.siblings("p.summary")
.text();
if (title && link) {
// Insert the data in the scrapedData db
// Create a new Article using the `result` object built from scraping
db.Article.create(result)
.then(function (dbArticle) {
// View the added result in the console
// console.log(dbArticle);
})
.catch(function (err) {
console.log(err);
// If an error occurred, send it to the client
//return res.json(err);
});
}
});
// If we were able to successfully scrape and save an Article, send a message to the client
console.log("Scrape Complete");
res.redirect("/articles");
});
});
// Route for getting all Articles from the db
app.get("/articles", function (req, res) {
db.Article.find({})
.then(function (dbArticle) {
// console.log(dbArticle);
res.render("index", { dbArticle });
})
.catch(function (err) {
// If an error occurs, send the error back to the client
res.json(err);
});
});
// Route for deleting saved article from main article db
app.delete("/delete/:id", function (req, res) {
db.Saved.findByIdAndRemove({ _id: req.params.id })
.then(function (dbArticle) {
})
.catch(function (err) {
// If an error occurs, send the error back to the client
res.json(err);
});
});
// Route for saving an article
app.post("/saved", function (req, res) {
db.Saved.create(req.body)
.then(function (dbSaved) {
// View the added result in the console
// console.log(dbSaved);
})
.catch(function (err) {
// console.log(err);
// If an error occurred, send it to the client
//return res.json(err);
});
});
app.get("/saved", function (req, res) {
db.Saved.find({})
.then(function (savedArticle) {
// If all Notes are successfully found, send them back to the client
res.render("saved", { savedArticle });
})
.catch(function (err) {
// If an error occurs, send the error back to the client
res.json(err);
});
});
// Route for grabbing a specific Article by id, populate it with it's note
app.get("/articles/:id", function(req, res) {
// Using the id passed in the id parameter, prepare a query that finds the matching one in our db...
db.Saved.findOne({ _id: req.params.id })
// ..and populate all of the notes associated with it
.populate("note")
.then(function(dbArticle) {
// If we were able to successfully find an Article with the given id, send it back to the client
res.json(dbArticle);
})
.catch(function(err) {
// If an error occurred, send it to the client
res.json(err);
});
});
// Route for saving/updating an Article's associated Note
app.post("/articles/:id", function(req, res) {
// Create a new note and pass the req.body to the entry
db.Note.create(req.body)
.then(function(dbNote) {
// If a Note was created successfully, find one Article with an `_id` equal to `req.params.id`. Update the Article to be associated with the new Note
// { new: true } tells the query that we want it to return the updated User -- it returns the original by default
// Since our mongoose query returns a promise, we can chain another `.then` which receives the result of the query
return db.Saved.findOneAndUpdate({ _id: req.params.id }, { note: dbNote._id }, { new: true });
})
.then(function(dbArticle) {
// If we were able to successfully update an Article, send it back to the client
res.json(dbArticle);
})
.catch(function(err) {
// If an error occurred, send it to the client
res.json(err);
});
});
// Start the server
app.listen(PORT, function () {
console.log("App running on port " + PORT + "!");
});