-
Notifications
You must be signed in to change notification settings - Fork 317
/
server.js
138 lines (105 loc) · 3.26 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
// server.js
// SERVER-SIDE JAVASCRIPT
/////////////////////////////
// SETUP and CONFIGURATION
/////////////////////////////
//require express in our app
var express = require('express'),
bodyParser = require('body-parser');
// generate a new express app and call it 'app'
var app = express();
// serve static files in public
app.use(express.static('public'));
// body parser config to accept our datatypes
app.use(bodyParser.urlencoded({ extended: true }));
////////////////////
// DATA
///////////////////
var books = [
{
_id: 15,
title: "The Four Hour Workweek",
author: "Tim Ferriss",
image: "https://s3-us-west-2.amazonaws.com/sandboxapi/four_hour_work_week.jpg",
release_date: "April 1, 2007"
},
{
_id: 16,
title: "Of Mice and Men",
author: "John Steinbeck",
image: "https://s3-us-west-2.amazonaws.com/sandboxapi/of_mice_and_men.jpg",
release_date: "Unknown 1937"
},
{
_id: 17,
title: "Romeo and Juliet",
author: "William Shakespeare",
image: "https://s3-us-west-2.amazonaws.com/sandboxapi/romeo_and_juliet.jpg",
release_date: "Unknown 1597"
}
];
var newBookUUID = 18;
////////////////////
// ROUTES
///////////////////
// define a root route: localhost:3000/
app.get('/', function (req, res) {
res.sendFile('views/index.html' , { root : __dirname});
});
// get all books
app.get('/api/books', function (req, res) {
// send all books as JSON response
console.log('books index');
res.json(books);
});
// get one book
app.get('/api/books/:id', function (req, res) {
// find one book by its id
console.log('books show', req.params);
for(var i=0; i < books.length; i++) {
if (books[i]._id === req.params.id) {
res.json(books[i]);
break; // we found the right book, we can stop searching
}
}
});
// create new book
app.post('/api/books', function (req, res) {
// create new book with form data (`req.body`)
console.log('books create', req.body);
var newBook = req.body;
newBook._id = newBookUUID++;
books.push(newBook);
res.json(newBook);
});
// update book
app.put('/api/books/:id', function(req,res){
// get book id from url params (`req.params`)
console.log('books update', req.params);
var bookId = req.params.id;
// find the index of the book we want to remove
var updateBookIndex = books.findIndex(function(element, index) {
return (element._id === parseInt(req.params.id)); //params are strings
});
console.log('updating book with index', deleteBookIndex);
var bookToUpdate = books[deleteBookIndex];
books.splice(updateBookIndex, 1, req.params);
res.json(req.params);
});
// delete book
app.delete('/api/books/:id', function (req, res) {
// get book id from url params (`req.params`)
console.log('books delete', req.params);
var bookId = req.params.id;
// find the index of the book we want to remove
var deleteBookIndex = books.findIndex(function(element, index) {
return (element._id === parseInt(req.params.id)); //params are strings
});
console.log('deleting book with index', deleteBookIndex);
var bookToDelete = books[deleteBookIndex];
books.splice(deleteBookIndex, 1);
res.json(bookToDelete);
});
app.listen(process.env.PORT || 3000, function () {
console.log('Book app listening at http://localhost:3000/');
});