-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
179 lines (154 loc) · 5.24 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
var express = require('express');
var cors = require('cors');
var bodyParser = require('body-parser');
var mongodb = require('mongodb');
var app = express();
var request = require('request');
PORT = process.env.PORT || 3000;
app.use(cors());
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
// connect to the database server
var MongoClient = mongodb.MongoClient;
var GOOGLE_BOOKS_API = process.env.GOOGLE_BOOKS_API;
// var url = 'mongodb://heroku_jp0xr3w7:juearsuhnp8gppac1n7t31s4bq@ds029745.mlab.com:29745/heroku_jp0xr3w7/books_db';
var url = 'mongodb://localhost:27017/books_db';
mongodb.MongoClient.connect(process.env.MONGODB_URI || url, function (err, database) {
if (err) {
console.log(err);
process.exit(1);
}
});
// THANK GOD for unicorns demo & my marvel hw for the skelton of the code below.
// testing
app.get("/", function(req, res){
res.json({"description":"HI TIFFANY"});
});
//get all books
app.get("/books", function(req, res){
MongoClient.connect(mongoUrl, function (err, db) {
var favbooksCollection = db.collection('books');
if (err) {
console.log('Unable to connect to the mongoDB server. ERROR:', err);
} else {
/* Get all */
favbooksCollection.find().toArray(function (err, result) {
if (err) {
console.log("ERROR!", err);
response.json("error");
} else if (result.length) {
console.log('Found:', result);
response.json(result);
} else { //
console.log('No document(s) found with defined "find" criteria');
response.json("nothing found");
}
db.close(function() {
console.log( "database CLOSED");
});
}); // end find
} // end else
}); // end mongo connect
}); // end get all
//add favorite books
app.post("/books/favorites", function(req, res){
MongoClient.connect(mongoUrl, function (err, db) {
var favbooksCollection = db.collection('books');
if (err) {
console.log('Unable to connect to the mongoDB server. ERROR:', err);
} else {
// We are connected!
console.log('Connection established to', mongoUrl);
console.log('add new book!');
// Insert new favorite book!
var newFavoriteBook = request.body;
favbooksCollection.insert(data, function (err, result) {
if (err) {
console.log(err);
response.json("error");
} else {
console.log('RESULT!!!!', result);
response.json(result);
}
db.close(function() {
console.log( "database CLOSED");
});
}); // end insert
} // end else
}); // end mongo connect
}); // end add new
// find book
app.get('/books/:name', function(req, res){
// response.json({"description":"find by name"});
// console.log("request.params: ", request.params);
MongoClient.connect(mongoUrl, function (err, db) {
var favbooksCollection = db.collection('books');
if (err) {
console.log('Unable to connect to the mongoDB server. ERROR:', err);
} else {
// We are connected!
console.log('Finding your saved book... ');
/* Get */
favebooksCollection.find(request.params).toArray(function (err, result) {
if (err) {
console.log("ERROR!", err);
res.json("error");
} else if (result.length) {
console.log('Found:', result);
res.json(result);
} else { //
console.log('No document(s) found with defined "find" criteria');
res.json("no books found");
}
db.close(function() {
console.log( "database CLOSED");
});
}); // end find
} // end else
}); // end mongo connect
});
// delete favorite book!
app.delete('/favorites/:_id', function(req, res) {
// console.log("request.body:", request.body);
console.log("request.params:", request.params._id);
// console.log(request.body.id);
MongoClient.connect(mongoUrl, function (err, db) {
var favbooksCollection = db.collection('books');
if (err) {
console.log('Unable to connect to the mongoDB server. ERROR:', err);
} else {
console.log('Connection established to', mongoUrl);
console.log('Goodbye book...');
favbooksCollection.remove({_id: ObjectId(request.body.id)}, function(err, result) {
if (err) {
console.log("ERROR!", err);
res.json("error");
} else if (result.length) {
console.log('Found:', result);
res.json(result);
} else {
console.log('No document(s) found with defined "find" criteria');
res.json("none found");
}
db.close(function() {
console.log( "database CLOSED");
});
});//end remove
} // end else
}); // end mongo connect
});//Delete Closing Tags
app.post("/", function(req, res){
// stuff will go here
var searchValue = req.body.searchValue;
var chosenEndPoint = req.body.chosenEndPoint;
request({
url: "https://www.googleapis.com/books/v1/volumes?q=search+terms" + searchValue,
method: "get",
callback: function(error, response, body){
res.send(body);
}
});
});//end post
app.listen(PORT, function(){
console.log('listening...');
});