-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
55 lines (47 loc) · 1.5 KB
/
Copy pathserver.js
File metadata and controls
55 lines (47 loc) · 1.5 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
const express = require("express");
const cors = require("cors");
const { isExpired } = require("./scripts/checkExpiry");
const { main } = require("./scripts/writeFile");
const dotenv = require("dotenv");
const fetch = require("node-fetch");
const { put } = require("@vercel/blob");
dotenv.config();
const app = express();
const blobStorage = process.env.blobUrl;
// Middleware
app.use(cors()); // Enable CORS for all routes
// Default route to redirect to /scholar
app.get("/", (req, res) => {
res.redirect("/scholar");
});
// Route to serve data from /scholar
app.get("/scholar", async (req, res) => {
try {
const expired = await isExpired();
if (expired) {
await main(); // Update data if expired
await put(process.env.expiryFilePath, new Date().toISOString(), {
access: "public",
contentType: "text/plain",
allowOverwrite: true,
});
}
const response = await fetch(blobStorage);
if (!response.ok) {
throw new Error(
`Failed to fetch blob data. HTTP status: ${response.status}`
);
}
const blobData = await response.json();
res.json(blobData); // Send JSON data as response
} catch (error) {
console.error("Error:", error);
res.status(500).json({ error: "Failed to process request" }); //
}
});
// Start the server
const PORT = process.env.PORT || 5000; // Use environment variable or default port
app.listen(PORT, () => {
console.log(`Server is listening on http://localhost:${PORT}`);
});
module.exports = app;