-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
119 lines (100 loc) · 3.55 KB
/
server.js
File metadata and controls
119 lines (100 loc) · 3.55 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
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
// Importing required libraries
import express from 'express';
import cors from 'cors';
import fetch from 'node-fetch';
import { MongoClient, ServerApiVersion } from 'mongodb';
// Create Express instance
const app = express();
// Define port
const port = process.env.PORT || 3001;
// MongoDB
const uri = "redacted";
const client = new MongoClient(uri, { useNewUrlParser: true, useUnifiedTopology: true, serverApi: ServerApiVersion.v1 });
await client.connect();
// Connect to MongoDB
async function connectToMongoDB(){
try{
await client.connect();
console.log("Connected to MongoDB");
}
catch (error) {
console.error("Error connecting to MongoDB:", error);
}
}
connectToMongoDB();
// Middleware
app.use(cors());
app.use(express.json());
// Define home route
app.get('/', (req, res) => {
res.send('Server home route');
});
// Route to fetch api books. Gets 10 books max.
app.get('/api/books', async (req, res) => {
const query = req.query.q;
const startIndex = req.query.startIndex || 0; // Get startIndex from query params, default to 0
const maxResults = req.query.maxResults || 10; // Get maxResults from query params, default to 10
if (!query) {
return res.status(400).send('Query is required');
}
try {
const response = await fetch(`https://www.googleapis.com/books/v1/volumes?q=${query}&startIndex=${startIndex}&maxResults=${maxResults}`);
const data = await response.json();
res.json(data);
} catch (error) {
console.error('Error fetching data from Google Books API:', error);
res.status(500).send('Error fetching data from Google Books API');
}
});
// Route to fetch book details by ID
app.get('/api/book/:id', async (req, res) => {
const bookId = req.params.id;
try {
const response = await fetch(`https://www.googleapis.com/books/v1/volumes/${bookId}`);
if (!response.ok) {
return res.status(404).send('Book not found');
}
const data = await response.json();
res.json(data);
} catch (error) {
console.error('Error fetching data from Google Books API:', error);
res.status(500).send('Error fetching data from Google Books API');
}
});
// Route to fetch saved books
app.get('/api/saved-books', async (req, res) => {
try {
const db = await client.db("SavedBooks");
const collection = db.collection("bookCollection");
const savedBooks = await collection.find({}).toArray();
res.json(savedBooks);
}
catch(error){
console.error('Error fetching saved books from MongoDB', error);
res.status(500).send('Error fetching saved books from MongoDB');
}
})
// Add book to SavedBooks database
app.post('/api/books', async (req,res) => {
const book = req.body;
try {
const db = client.db("SavedBooks");
const collection = db.collection("bookCollection");
const result = await collection.insertOne(book);
console.log(`Book added with ID: ${result.insertedId}`);
res.status(201).json({ message: 'Book added successfully', bookId: result.insertedId });
} catch (error) {
console.error('Error adding book to MongoDB:', error);
res.status(500).send('Error adding book to MongoDB');
}
})
// Close MongoDB connection when the server shuts down
process.on('SIGINT', async () => {
await client.close();
console.log("MongoDB connection closed");
process.exit(0);
});
// Listen on selected port
app.listen(port, () => {
console.log(`Server is running in port ${port}`);
});