-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
58 lines (50 loc) · 1.52 KB
/
index.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
const express = require('express');
const fs = require('fs');
require('dotenv').config()
const app = express();
app.use(express.static(__dirname + '/public'));
app.set('view engine', 'ejs');
app.set('views', './views');
let rawdata = fs.readFileSync(__dirname + '/data/data.json');
const data = JSON.parse(rawdata);
let categoryData = {};
for (let book of data) {
splited = book.author.split('Visite a página de ')
if (splited.length > 1)
book.author = splited[1]
for (let category of book.categories) {
if (!(category in categoryData))
categoryData[category] = [book];
else
categoryData[category].push(book);
}
}
const PORT = process.env.PORT || 80;
app.get('/', (req, res) => {
if (req.query.search) {
let search = req.query.search;
let results = [];
for (let book of data) {
let title = book.title;
if (title.toLowerCase().match(`^.*${search.toLowerCase()}.*$`))
results.push(book);
}
res.render('search.ejs', {results: results});
}
else
res.render('index.ejs', {categories: categoryData});
})
app.get('/:title', (req, res) => {
let desiredBook = null;
for (let book of data) {
if (book.title === req.params.title)
desiredBook = book;
}
if (desiredBook === null)
res.render('notfound.ejs');
else
res.render('book.ejs', desiredBook);
})
app.listen(PORT, () => {
console.log(`Server listening on http://localhost:${PORT}`);
});