-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
73 lines (52 loc) · 1.98 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
'use strict'
require('dotenv').config();
const express = require('express');
const superagent = require('superagent');
const PORT = process.env.PORT || 4000;
const app = express();
app.use(express.static('./public'));
app.use(express.urlencoded({ extended: true }));
app.set('view engine', 'ejs');
app.get('/', (req, res) => {
res.render('./pages/index')
});
app.get('/searches/new', (req, res) => {
res.render('./pages/searches/new');
});
app.post('/searches', (req, res) => {
// console.log(req.body);
const title = req.body.keyword;
const searchBy = req.body.searchBy;
// console.log(title)
const url = `https://www.googleapis.com/books/v1/volumes?q=${title}+in${searchBy}:${title}`;
superagent.get(url).then(apiData => {
const books = [];
apiData.body.items.forEach((bookItem, index) => {
if (index < 10) {
const book = new Book(bookItem);
// console.log(book);
books.push(book);
// console.log(books);
}
});
// console.log(apiData.body.items[0].volumeInfo);
res.render('./pages/searches/show', {books: books});
}).catch((err) => {
errorHandler(err, req, res);
});
});
app.use('*', notFoundHandler);
app.use(errorHandler);
function Book(bookData) {
this.title = bookData.volumeInfo.title;
this.authors = bookData.volumeInfo.authors.join(', ');
bookData.volumeInfo.description != undefined ? this.description = bookData.volumeInfo.description : this.description = 'Discription goes here...'
bookData.volumeInfo.imageLinks != undefined ? this.imageUrl = bookData.volumeInfo.imageLinks.thumbnail.replace('http', 'https') : this.imageUrl = 'filler'
}
function errorHandler(error, req, res) {
res.status(500).render('./pages/error', {error: error});
}
function notFoundHandler(req, res) {
res.status(404).send('NOT FOUND!!');
}
app.listen(PORT, () => console.log(`We're live on port ${PORT} BB ^ o ^`));