-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
163 lines (133 loc) · 3.84 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
// Dependencies
const path = require("path");
const express = require("express");
const logger = require("morgan");
const request = require("request");
const cheerio = require("cheerio");
const mongoose = require("mongoose");
const bodyParser = require("body-parser");
const exphbs = require("express-handlebars");
const env = process.env.NODE_ENV
mongoose.Promise = Promise;
if (env === 'development')
// development env variables
require('dotenv').config();
// Require our models
const Article = require("./models/Articles.js");
const Note = require("./models/Notes.js")
// Initialize Express
const app = express();
app.use(logger("dev"));
app.use(bodyParser.urlencoded({ extended: false }));
// server public directory
app.use(express.static("public"));
// Template engine setup : Handlebars
app.engine("handlebars", exphbs({ defaultLayout: "main", layoutsDir: "./views/layouts/"}));
app.set("view engine", "handlebars");
app.set("views", path.join(__dirname, "./views"))
/****************************************
Database configuration with mongoose
****************************************/
const conStr = process.env.NODE_ENV === "development"
? process.env.MONGO_URI
: process.env.MONGODB_URI
mongoose.connect(conStr);
const db = mongoose.connection;
db.on("error", function(error) {
console.log("Mongoose Error: ", error);
});
db.once("open", function() {
console.log(`Mongoose connected on port ${db.port}.`);
});
//------------------------------------------------------------------------------
// Routes
// ======
app.get("/", function(req,res) {
Article.find({}, function(error, docs) {
if (error) {
console.log(error);
}
else {
res.render('index', {articles: docs});
}
});
});
// endpoint to activate scraper and store in DB
app.get("/scrape-it", function(req, res) {
request("https://www.nyunews.com/category/arts/", function(error, response, html) {
var $ = cheerio.load(html);
var stuff = [];
$(".sno-animate").each(function(i, element) {
let result = {};
const classList = $(this).attr('class').split(/\s+/);
if (classList.length === 1) {
result.title = $(this).children(".searchheadline").text();
result.link = $(this).children("a").attr("href");
result.date = $(this).children($("p:nth-child(3)")).children("span.time-wrapper").text();
result.preview = $(this).find("p:nth-child(4)").text();
stuff.push(result);
}
const entry = new Article(result);
entry.save(function(err, doc) {
if (err) {
console.log(err);
}
else {
console.log(doc);
}
});
});
console.log(stuff)
});
res.send("Scrape Complete");
});
// Get all articles
app.get("/api/articles", function(req, res) {
Article.find({}, function(error, doc) {
if (error) {
console.log(error);
}
else {
res.json(doc);
}
});
});
// Get article by ObjectId
app.get("/api/articles/:id", function(req, res) {
Article.findOne({ "_id": req.params.id })
.populate("note")
.exec(function(error, doc) {
if (error) {
console.log(error);
}
else {
res.json(doc);
}
});
});
// Create a new note or replace an existing note
app.post("/api/articles/:id", function(req, res) {
var newNote = new Note(req.body);
// And save the new note the db
newNote.save(function(error, doc) {
if (error) {
console.log(error);
}
else {
Article.findOneAndUpdate({ "_id": req.params.id }, { "note": doc._id })
.exec(function(err, doc) {
if (err) {
console.log(err);
}
else {
res.send(doc);
}
});
}
});
});
const port = process.env.PORT;
// Listen on port 3000
app.listen(port, function() {
console.log("App running on port 3000!");
});