forked from FaustineG/inkto-rever
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
52 lines (44 loc) · 1.27 KB
/
index.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
51
52
const express = require("express");
const fs = require("fs");
const app = express();
const PORT = 3030;
let dailyWord = null;
let lastUpdated = null;
app.use(express.static("./"));
// Function to load the daily word from a file
const loadDailyWord = () => {
if (fs.existsSync("dailyWord.json")) {
const data = JSON.parse(fs.readFileSync("dailyWord.json", "utf8"));
dailyWord = data.word;
lastUpdated = new Date(data.date);
} else {
updateDailyWord();
}
};
// Function to update the daily word and save it to a file
const updateDailyWord = () => {
const words = ["apple", "banana", "cherry", "date", "elderberry"];
dailyWord = words[Math.floor(Math.random() * words.length)];
lastUpdated = new Date();
fs.writeFileSync(
"dailyWord.json",
JSON.stringify({ word: dailyWord, date: lastUpdated })
);
};
// Middleware to check if we need to update the daily word
app.use((req, res, next) => {
const now = new Date();
if (!dailyWord || now.getDate() !== lastUpdated.getDate()) {
updateDailyWord();
}
next();
});
// Route to get the daily word
app.get("/word", (req, res) => {
res.json({ word: dailyWord });
});
// Load the word initially
loadDailyWord();
app.listen(PORT, () => {
console.log(`Server is running on http://localhost:${PORT}`);
});