-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
167 lines (140 loc) · 4.4 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
// Dependencies
var express = require("express");
var bodyParser = require("body-parser");
var logger = require("morgan");
var mongoose = require("mongoose");
// 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;
var MONGODB_URI = process.env.MONGODB_URI || "mongodb://127.0.0.1/heroku_hhxq80sg";
// // Database configuration with mongoose
// mongoose.connect("mongodb://heroku_hhxq80sg:6711ls7b4tf0j1cjd084tsofte@ds259351.mlab.com:59351/heroku_hhxq80sg");
var db = mongoose.connection;
// Connect to the Mongo DB
mongoose.connect(MONGODB_URI);
// Use morgan and body parser with our app
app.use(logger("dev"));
app.use(bodyParser.urlencoded({
extended: false
}));
// Make public a static dir
app.use(express.static("public"));
// 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
// ======
// home page
// app.get("/", function(req, res)
app.get('../', function(req, res) {
res.send(html);
});
// A GET request to scrape the screenrant website
app.get("/scrape", function(req, res) {
// First, we grab the body of the html with request
request("https://www.screenrant.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 h3 within an article tag, and do the following:
$("h3.bc-title").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");
// 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);
}
});
});
});
// Tell the browser that we finished scraping the text
res.send("Scrape Complete");
});
// This will get the articles we scraped from the mongoDB
app.get("/articles", function(req, res) {
// Grab every doc in the Articles array
Article.find({}, function(error, doc) {
// Log any errors
if (error) {
console.log(error);
}
// Or send the doc to the browser as a json object
else {
res.json(doc);
}
});
});
// Grab an article by it's ObjectId
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...
Article.findOne({ "_id": req.params.id })
// ..and populate all of the notes associated with it
.populate("note")
// now, execute our query
.exec(function(error, doc) {
// Log any errors
if (error) {
console.log(error);
}
// Otherwise, send the doc to the browser as a json object
else {
res.json(doc);
}
});
});
// Create a new note or replace an existing note
app.post("/articles/:id", function(req, res) {
// Create a new note and pass the req.body to the entry
var newNote = new Note(req.body);
// And save the new note the db
newNote.save(function(error, doc) {
// Log any errors
if (error) {
console.log(error);
}
// Otherwise
else {
// Use the article id to find and update it's note
Article.findOneAndUpdate({ "_id": req.params.id }, { "note": doc._id })
// Execute the above query
.exec(function(err, doc) {
// Log any errors
if (err) {
console.log(err);
}
else {
// Or send the document to the browser
res.send(doc);
}
});
}
});
});
// Listen on port 3000
app.listen(port, function() {
console.log("App running on "+port+"!");
});