-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathapp.js
50 lines (39 loc) · 1.08 KB
/
app.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
const express = require("express");
const cors = require("cors");
const dotenv = require("dotenv");
const axios = require("axios");
const NodeCache = require("node-cache");
dotenv.config();
const app = express();
const cache = new NodeCache();
const corsOptions = {
origin: "https://yourdomain.com",
optionsSuccessStatus: 200,
};
app.use(cors(corsOptions));
app.get("/books", async (req, res) => {
try {
const cacheKey = "books";
const cachedData = cache.get(cacheKey);
if (cachedData) {
return res.json(cachedData);
}
const apiKey = process.env.API_KEY;
const headers = {
Authorization: `Bearer ${apiKey}`,
};
const response = await axios.get(
"https://api.airtable.com/v0/{baseid}/{tablename}",
{ headers }
);
cache.set(cacheKey, response.data, 3600);
res.json(response.data);
} catch (error) {
console.error("Error:", error);
res.status(500).json({ message: "Internal Server Error" });
}
});
const port = process.env.PORT || 3000;
app.listen(port, () => {
console.log(`Server running on port ${port}`);
});