-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
228 lines (188 loc) · 6.75 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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
var express = require("express");
var logger = require("morgan");
var mongoose = require("mongoose");
var mongojs = require("mongojs");
var exphbs = require("express-handlebars");
// 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"));
// Handlebars
app.engine(
"handlebars",
exphbs({
defaultLayout: "main"
})
);
app.set("view engine", "handlebars");
var databaseURI = "mongodb://localhost/scrapinghw";
if (process.env.MONGODB_URI) {
mongoose.connect(process.env.MONGODB_URI/*, { useNewUrlParser: true }*/);
} else {
mongoose.connect(databaseURI, { useNewUrlParser: true });
}
// Routes
app.get("/", function(req, res) {
res.render("index");
});
// 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("https://www.sitepoint.com/community/c/javascript").then(function(response) {
// Then, we load that into cheerio and save it to $ for a shorthand selector
console.log(response.data);
var $ = cheerio.load(response.data);
// Now, we grab every h2 within an article tag, and do the following:
$("div").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")
.children("span")
.text();
result.link = $(this)
.children("a")
.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);
});
});
});
res.json("dummy");
});
// 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);
});
});
// Route for deleting an Article's associated Note
app.put("/articles/:id", function(req, res) {
// Create a new note and pass the req.body to the entry
console.log(req.body);
db.Note.findOne({title : req.body.title/*, body : req.body.body*/})
.then(function(dbNote) {
console.log("dbNote._id: " + dbNote._id);
db.Note.deleteOne({_id: mongojs.ObjectId(dbNote._id)})
.then(function(result) {
if (result.deletedCount === 0) {
throw new Error('No document to delete with ID: ' + _id);
}
console.log(req.params.id);
db.Article.updateOne(
{ _id: mongojs.ObjectId(req.params.id) },
{ $unset: { note: "" } }
)
.then(function(error, edited) {
if (error) throw error;
else
console.log(edited);
res.send(edited)
});
})
.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);
});
});
});
// Route for grabbing a specific Note by id, populate it with it's note
app.get("/notes/:id", function(req, res) {
// Using the id passed in the id parameter, prepare a query that finds the matching one in our db...
db.Note.findOne({ _id: req.params.id })
// ..and populate all of the notes associated with it
.then(function(dbNote) {
// If we were able to successfully find an Article with the given id, send it back to the client
res.json(dbNote);
})
.catch(function(err) {
// If an error occurred, send it to the client
res.json(err);
});
});
// Route for deleting all articles from db
app.put("/clear", function(req, res) {
// Create a new note and pass the req.body to the entry
console.log(req.body);
db.Article.deleteMany({})
.then(function(result) {
console.log("Deleted "+result.deletedCount+" articles");
})
.catch(function(err) {
// If an error occurred, send it to the client
res.json(err);
});
});
const host = '0.0.0.0';
// Start our server so that it can begin listening to client requests.
app.listen(PORT, host, function() {
// Log (server-side) when our server has started
console.log("Server listening on: http://localhost:" + PORT);
});