-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
129 lines (109 loc) · 4.56 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
var express = require("express");
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"));
// Parse request body as JSON
app.use(express.urlencoded({ extended: true }));
app.use(express.json());
// Make public a static folder
app.use(express.static("public"));
// Connect to the Mongo DB
const MONGODB_URI = process.env.MONGODB_URI || "mongodb://localhost/trumpy";
mongoose.connect(MONGODB_URI, { useUnifiedTopology: true, useNewUrlParser: 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 axios
axios.get("http://www.trump-news.today/").then(function (response) {
// Then, we load that into cheerio and save it to $ for a shorthand selector
var $ = cheerio.load(response.data);
console.log($);
// Now, we grab every h2 within an article tag, and do the following:
$("i").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.article_title")
.text();
result.link = $(this)
.children("a.article_title")
.attr("href");
// 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) {
// If an error occurred, log it
console.log(err);
});
});
// Send a message to the client
res.redirect("/");
});
});
// Route for getting all Articles from the db
app.get("/articles", function (req, res) {
// Grab every document in the Articles collection
db.Article.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.Article.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.Article.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 + "!");
});