-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
25 lines (21 loc) · 811 Bytes
/
Copy pathserver.js
File metadata and controls
25 lines (21 loc) · 811 Bytes
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
import express from 'express';
import bodyParser from 'body-parser';
import fetch from 'node-fetch';
const app = express();
const PORT = process.env.PORT || 3000;
app.use(bodyParser.json());
app.use(express.static('public'));
app.get('/search', async (req, res) => {
const query = req.query.q;
const startIndex = req.query.startIndex || 0; // Default to 0 if not provided
try {
const response = await fetch(`https://www.googleapis.com/books/v1/volumes?q=${query}&startIndex=${startIndex}&maxResults=10`);
const data = await response.json();
res.json(data);
} catch (error) {
res.status(500).json({ error: 'Failed to fetch data from Google Books API' });
}
});
app.listen(PORT, () => {
console.log(`Server is running on http://localhost:${PORT}`);
});