-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
215 lines (193 loc) · 6.35 KB
/
Copy pathserver.js
File metadata and controls
215 lines (193 loc) · 6.35 KB
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
var express = require("express");
var bodyParser = require("body-parser");
// var logger = require("morgan");
var mongoose = require("mongoose");
var request = require("request");
// 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 = 3000;
// Initialize Express
var app = express();
// Configure middleware
// Use morgan logger for logging requests
// app.use(logger("dev"));
// Use body-parser for handling form submissions
app.use(
bodyParser.urlencoded({
extended: true
})
);
// Use express.static to serve the public folder as a static directory
app.use(express.static("public"));
// Connect to the Mongo DB
// mongoose.connect("mongodb://localhost/ebayScraper");
mongoose.connect("mongodb://heroku_kqxh70dp:12arfqkbguodmuu8uognl1cl8j@ds261450.mlab.com:61450/heroku_kqxh70dp")
// Routes
app.get('/', function() {
res.sendFile(index.html);
});
// A GET route for scraping the ebay website
app.get("/scrape", function(req, res) {
// grab the body of the html with axios
// console.log("scraping....")
request(
"https://www.ebay.com/b/Laptops-Netbooks/175672/bn_1648276?LH_Auction=1",
function(error, response, html) {
// Load the HTML into cheerio and save it to a variable
var $ = cheerio.load(html);
// get relevant information from ebay item listings.
$("div.s-item__wrapper.clearfix").each(function(i, element) {
var result = {};
result.title = $(element)
.find("div.s-item__info")
.find("a")
.find("h3.s-item__title")
.text();
result.subtitle = $(element)
.find("div.s-item__info")
.find("div.s-item__subtitle")
.text();
result.link = $(element)
.find("div.s-item__info")
.find("a")
.attr("href");
result.price = $(element)
.find("div.s-item__info")
.find("div.s-item__details")
.find("div.s-item__detail")
.find("span.s-item__price")
.text();
result.imgSource = $(element)
.find("div.s-item__image-section")
.find("div.s-item__image")
.find("a")
.find("div.s-item__image-wrapper")
.find("img")
.attr("src");
// Create a new Item using the `result` object built from scraping
db.Item.create(result)
.then(function(dbItem) {
// not doing anything Headers
})
.catch(function(err) {
// If an error occurred, send it to the client
console.log(err);
});
// If we were able to successfully scrape and save an auction item, send a message to the client
});
res.redirect("/");
}
);
});
// Route for getting all auction items from the db
app.get("/items", function(req, res) {
// Grab every document in the auction items collection
db.Item.find({})
.then(function(dbItem) {
// If we were able to successfully find auction items, send them back to the client
res.json(dbItem);
})
.catch(function(err) {
// If an error occurred, send it to the client
res.json(err);
});
});
//delete inidicated item from database.
app.get("/deleteItem/:id", function(req, res) {
db.Item.findByIdAndRemove(req.params.id, () => {
// res.redirect('/');
// db.Item.deleteOne({_id:req.params.id});
});
res.redirect("/");
});
app.post("/deleteAllItems/", function(req, res) {
db.Item.deleteMany({})
.then(() => {
console.log("deleting");
})
.then(response => {
res.redirect("/");
});
});
// Route for grabbing a specific auction item by id, populate it with it's note
app.get("/items/:id", function(req, res) {
// Using the id passed in the id parameter, prepare a query that finds the matching one in our db...
db.Item.findOne({
_id: req.params.id
})
// ..and populate all of the notes associated with it
.populate("note")
.then(function(dbItem) {
// If we were able to successfully find an auction item with the given id, send it back to the client
res.json(dbItem);
})
.catch(function(err) {
// If an error occurred, send it to the client
res.json(err);
});
});
// Route for saving/updating an auction item's associated Note
app.post("/items/: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 auction item with an `_id` equal to `req.params.id`. Update the auction item 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.Item.findOneAndUpdate(
{
_id: req.params.id
},
{
note: dbNote._id
},
{
new: true
}
);
})
.then(function(dbItem) {
// If we were able to successfully update an auction item, send it back to the client
res.json(dbItem);
})
.catch(function(err) {
// If an error occurred, send it to the client
res.json(err);
});
});
// Route for deleting an auction item's associated Note
app.post("/deleteNote/:id", function(req, res) {
// Create a new note and pass the req.body to the entry
db.Note.findByIdAndRemove(req.params.id, () => {
})
.then(function(dbItem) {
// If we were able to successfully update an auction item, send it back to the client
res.redirect("/");
})
.catch(function(err) {
// If an error occurred, send it to the client
// res.json(err);
// });
});
});
// Route for deleting an auction item's associated Note
app.post("/deleteAllNotes/", function(req, res) {
// Create a new note and pass the req.body to the entry
console.log("made it to delete all notes");
db.Note.deleteMany({})
.then(() => {
console.log("deleting all notes");
})
.then(response => {
res.redirect("/");
});
});
// Start the server
app.listen(PORT, function() {
console.log("App running on port " + PORT + "!");
});