-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
167 lines (141 loc) · 5.82 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
var express = require("express");
var bodyParser = require("body-parser");
var logger = require("morgan");
var mongoose = require("mongoose");
// Our scraping tools
// Axios is a promised-based http library, similar to jQuery's Ajax method
// It works on the client and on the server
var axios = require("axios");
var cheerio = require("cheerio");
// Require all models
var db = require("./models");
var PORT = process.env.PORT || 3000;
// Initialize Express
var app = express();
// Configure middleware
// Use morgan logger for logging requests
app.use(logger("dev"));
// Use body-parser for handling form submissions
app.use(bodyParser.urlencoded({ extended: true }));
// Use express.static to serve the public folder as a static directory
app.use(express.static("public"));
// By default mongoose uses callbacks for async queries, we're setting it to use promises (.then syntax) instead
// Connect to the Mongo DB
mongoose.Promise = Promise;
mongoose.connect(process.env.MONGODB_URI || "mongodb://localhost/news1", {
useMongoClient: true
});
// Routes
// A GET route for scraping the echojs website
app.get("/scrape", function(req, res) {
// First, we grab the body of the html with request
axios.get("https://www.theverge.com/tech").then(function(response) {
// Then, we load that into cheerio and save it to $ for a shorthand selector
var $ = cheerio.load(response.data);
// Now, we grab every h2 within an article tag, and do the following:
$("div.c-entry-box--compact.c-entry-box--compact--article div h2 a").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)
// .children("a")
// .text();
// result.link = $(this)
// .children("a")
// .attr("href");
result.title = $(this).text();
result.link = $(this).attr('href');
// Create a new Article using the `result` object built from scraping
db.Headline.create(result)
.then(function(dbArticle) {
// View the added result in the console
console.log(dbArticle);
})
.catch(function(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
res.send("Scrape Complete");
});
});
// app.get("/scrape", function (req, res) {
// axios.get('https://www.theverge.com/tech').then(function (response) {
// var $ = cheerio.load(response.data);
// $("div.c-entry-box--compact.c-entry-box--compact--article div h2 a").each(function (i, element) {
// var result = {};
// result.headline = $(this).text();
// result.url = $(this).attr('href');
// if (i === 0) {
// console.log(result);
// db.Headline.create(result)
// .then(function(dbHeadline){
// console.log(dbHeadline);
// })
// .catch(function(err){
// return res.json(err);
// });
// // console.log(element.parent.parent.parent);
// // console.log(element.children[0].data);
// // console.log($(this).attr('href'));
// // console.log($(this).text());
// // console.log($(this).children('a')[0].attribs);
// // console.log($(this).children('.c-entry-box--compact__title'));
// }
// // console.log(element.children[0].data);
// })
// res.send("Scrape Complete");
// })
// });
// Route for getting all Articles from the db
app.get("/articles", function(req, res) {
// Grab every document in the Articles collection
db.Headline.find({})
.then(function(dbArticle) {
// If we were able to successfully find Articles, send them back to the client
res.json(dbArticle);
})
.catch(function(err) {
// If an error occurred, send it 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.Headline.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.Headline.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 + "!");
});