Repository navigation
Expand file tree
/
Copy pathserver.js
More file actions
94 lines (77 loc) · 2.95 KB
/
Copy pathserver.js
File metadata and controls
94 lines (77 loc) · 2.95 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
import express from 'express';
import path from 'path';
import { fileURLToPath } from 'url';
import axios from 'axios';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const app = express();
const port = process.env.PORT || 3000;
// Basic auth credentials
const auth = {
username: 'wRe9kMfJ6r',
password: '2u3ECx9pJr57nXVz871XEq'
};
const API_BASE_URL = 'https://assess.virtual.blue';
app.use(express.static('public'));
const dataMap = new Map();
app.get('/api/books', async (req, res) => {
if(dataMap.size > 0) {
return res.json(Array.from(dataMap.values()));
}
try {
const response = await axios.get(`${API_BASE_URL}/books`, {
auth,
headers: {
'Accept': 'application/json'
}
});
const books = Array.isArray(response.data) ? response.data : [];
for (const book of books) {
try {
const bookDetailResponse = await axios.get(`${API_BASE_URL}/book/${book.isbn}`, {
auth,
headers: {
'Accept': 'application/json'
}
});
const detailedBook = bookDetailResponse.data;
const sanitizedBook = {
id: detailedBook.isbn,
title: String(detailedBook.title || 'Untitled Book'),
author: String(detailedBook.author || 'Unknown Author'),
publisher: String(detailedBook.publisher || 'Unknown Publisher'),
published: Number(detailedBook.published || 'Unknown Date'),
collection: String(detailedBook.collection || 'Unknown Collection'),
related: String(detailedBook.related || 'Unknown Related'),
height: Number(detailedBook.height.replace('cm', '').trim()) || 20,
width: Number(detailedBook.width.replace('cm', '').trim()) || 5,
};
dataMap.set(detailedBook.isbn, sanitizedBook);
} catch (error) {
console.error(`Error fetching details for book ${book.isbn}:`, error.response?.data || error.message);
}
}
res.json(Array.from(dataMap.values()));
} catch (error) {
console.error('Error fetching books:', error.response?.data || error.message);
res.status(500).json({ error: 'Failed to fetch books' });
}
});
app.get('/api/collection/:collection', async (req, res) => {
try {
const collectionName = req.params.collection;
console.log('collectionName:', collectionName);
if (collectionName === undefined) collectionName = 'history';
const booksInCollection = Array.from(dataMap.values()).filter(book => book.collection === collectionName);
res.json(booksInCollection);
} catch (error) {
console.error('Error fetching books in collection:', error.response?.data || error.message);
res.status(500).json({ error: 'Failed to fetch books in collection' });
}
});
app.get('/', (req, res) => {
res.sendFile(path.join(__dirname, 'public', 'index.html'));
});
app.listen(port, () => {
console.log(`Server running at http://localhost:${port}`);
});