-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
180 lines (158 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
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
// require dependencies
var express = require("express");
var mongoose = require("mongoose");
var request = require("request");
var cheerio = require("cheerio");
var bodyParser = require("body-parser");
var exphbs = require("express-handlebars");
var PORT = process.env.PORT || 3000;
// initialize Express
var app = express();
// use body-parser for handling form submissions
app.use(bodyParser.urlencoded({
extended: false
}));
app.use(bodyParser.json({
type: "application/json"
}));
// serve the public directory
app.use(express.static("public"));
// use promises with Mongo and connect to the database
var databaseUrl = "news";
mongoose.Promise = Promise;
var MONGODB_URI = process.env.MONGODB_URI || "mongodb://localhost/news";
mongoose.connect(MONGODB_URI);
// use handlebars
app.engine("handlebars", exphbs({
defaultLayout: "main"
}));
app.set("view engine", "handlebars");
// Hook mongojs configuration to the db variable
var db = require("./models");
// get all articles from the database that are not saved
app.get("/", function(req, res) {
db.Article.find({
saved: false
},
function(error, dbArticle) {
if (error) {
console.log(error);
} else {
res.render("index", {
articles: dbArticle
});
}
})
})
// use cheerio to scrape stories from TechCrunch and store them
app.get("/scrape", function(req, res) {
request("https://techcrunch.com/", function(error, response, html) {
// Load the html body from request into cheerio
var $ = cheerio.load(html);
$("div.post-block").each(function(i, element) {
// trim() removes whitespace because the items return \n and \t before and after the text
var title = $(element).find("a.post-block__title__link").text().trim();
var link = $(element).find("a.post-block__title__link").attr("href");
var intro = $(element).children(".post-block__content").text().trim();
// if these are present in the scraped data, create an article in the database collection
if (title && link && intro) {
db.Article.create({
title: title,
link: link,
intro: intro
},
function(err, inserted) {
if (err) {
// log the error if one is encountered during the query
console.log(err);
} else {
// otherwise, log the inserted data
console.log(inserted);
}
});
// if there are 10 articles, then return the callback to the frontend
console.log(i);
if (i === 10) {
return res.sendStatus(200);
}
}
});
});
});
// route for retrieving all the saved articles
app.get("/saved", function(req, res) {
db.Article.find({
saved: true
})
.then(function(dbArticle) {
// if successful, then render with the handlebars saved page
res.render("saved", {
articles: dbArticle
})
})
.catch(function(err) {
// If an error occurs, send the error back to the client
res.json(err);
})
});
// route for setting an article to saved
app.put("/saved/:id", function(req, res) {
db.Article.findByIdAndUpdate(
req.params.id, {
$set: req.body
}, {
new: true
})
.then(function(dbArticle) {
res.render("saved", {
articles: dbArticle
})
})
.catch(function(err) {
res.json(err);
});
});
// route for saving a new note to the db and associating it with an article
app.post("/submit/:id", function(req, res) {
db.Note.create(req.body)
.then(function(dbNote) {
var articleIdFromString = mongoose.Types.ObjectId(req.params.id)
return db.Article.findByIdAndUpdate(articleIdFromString, {
$push: {
notes: dbNote._id
}
})
})
.then(function(dbArticle) {
res.json(dbNote);
})
.catch(function(err) {
// If an error occurs, send it back to the client
res.json(err);
});
});
// route to find a note by ID
app.get("/notes/article/:id", function(req, res) {
db.Article.findOne({"_id":req.params.id})
.populate("notes")
.exec (function (error, data) {
if (error) {
console.log(error);
} else {
res.json(data);
}
});
});
app.get("/notes/:id", function(req, res) {
db.Note.findOneAndRemove({_id:req.params.id}, function (error, data) {
if (error) {
console.log(error);
} else {
}
res.json(data);
});
});
// listen for the routes
app.listen(PORT, function() {
console.log("App is running");
});