-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
196 lines (170 loc) · 5.8 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
// =====================================================================================
// DEPENDENCIES
// =====================================================================================
const express = require('express')
, exphbs = require('express-handlebars')
, bodyParser = require('body-parser')
, axios = require('axios')
, logger = require('morgan')
, cheerio = require('cheerio')
, request = require('request')
, mongoose = require('mongoose');
// set port
const port = process.env.PORT || 3000;
// require all models
const models = require('./models');
// =====================================================================================
// MIDDLEWARE
// =====================================================================================
// initialize express
const app = express();
// configure handlebars as view engine
app.engine('.hbs', exphbs({ extname: '.hbs', defaultLayout: 'main' }));
app.set('view engine', '.hbs');
// configure body parser to parse requests as json
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
// configure morgan to log requests to console
app.use(logger('dev'));
// serve up 'public' folder
app.use(express.static('public'));
// =====================================================================================
// MONGOOSE CONFIG
// =====================================================================================
// set up mongoose to leverage built-in JavaScript ES6 Promises
mongoose.Promise = Promise;
// if deployed, use the deployed database. else, use the local mongoHeadlines database.
var MONGODB_URI = process.env.MONGODB_URI || 'mongodb://localhost/mongoHeadlines';
// connect to the MongoDB
mongoose.connect(MONGODB_URI)
.then(() => {
console.log('Successfully connected to Mongo database');
})
.catch(err => {
console.error(err);
});
// =====================================================================================
// ROUTES
// =====================================================================================
app.get('/', (req, res) => {
models.Article.find({})
.then(dbArticles => {
res.render('index', { articles: dbArticles });
})
.catch(err => {
console.log(err);
});
});
app.get('/scrape', (req, res) => {
// models.Article.remove({}, () => {
// console.log('collection removed');
// });
var resultArr = [];
axios.get('https://news.ycombinator.com')
.then(response => {
var $ = cheerio.load(response.data);
$(".storylink").each(function(i, elem) {
var result = {};
result.title = $(this).text();
result.link = $(this).attr('href');
result.site = $(this).siblings('span').children('a').children('span').text();
resultArr.push(result);
});
console.log('resultArr length 1:');
console.log(resultArr.length); // 30
models.Article.insertMany(resultArr)
.then(newArticles => {
console.log('scrape complete!');
res.redirect('/');
})
.catch(err => {
res.redirect('/');
throw err;
// console.log(err.message);
// console.log(typeof err.message);
// let dupTitle = err.message.split('\"');
// console.log(dupTitle[1]);
// if (err.message.includes('E11000')) {
// // console.log('yo');
// models.Article.find({ title: dupTitle[1] })
// .then(dbArticle => {
// if (dbArticle) console.log(dbArticle);
// })
// .catch(err => {
// console.log(err);
// });
// }
});
})
.catch(err => {
res.send(err);
});
// THIS WILL CONSOLE OUT 0 BECAUSE THE AXIOS GET REQUEST IS ASYNCHRONOUS!!!
console.log('resultArr length 2:');
console.log(resultArr.length);
});
app.get('/saved', (req, res) => {
models.Article.find({ saved: true })
.then(savedArticles => {
res.render('saved', { articles: savedArticles });
})
.catch(err => {
console.log(err);
});
});
app.get('/api/articles/:id', (req, res) => {
id = req.params.id;
models.Article.findOneAndUpdate({ _id: id }, { saved: true }, { new: true })
.populate('note')
.then(dbArticle => {
res.send(dbArticle);
})
.catch(err => {
console.error(err);
});
})
app.post('/api/articles/:id', (req, res) => {
id = req.params.id;
models.Note.create(req.body)
.then(newNote => {
console.log(newNote);
return models.Article.findOneAndUpdate({ _id: id }, { note: newNote._id }, { new: true });
})
.then(updatedArticle => {
res.send(updatedArticle);
})
.catch(err => {
console.log(err);
});
});
app.get('/api/unsave/:id', (req, res) => {
id = req.params.id;
models.Article.findOneAndUpdate({ _id: id }, { $unset: { note: '', saved: '' }}, { new: true })
.then(unsavedArticle => {
console.log(`article ${unsavedArticle._id} saved: ${unsavedArticle.saved}`);
res.send(unsavedArticle);
})
.catch(err => {
console.error(err);
});
});
app.get('/api/clear', (req, res) => {
models.Article.remove({})
.then(articleResponse => {
return models.Note.remove({});
})
.then(noteResponse => {
console.log('articles & notes removed!');
res.send('articles & notes removed!');
})
.catch(err => {
console.log(err);
res.send(err);
});
});
// =====================================================================================
// LISTENING
// =====================================================================================
app.listen(port, () => {
console.log(`App running on port ${port}`);
});