-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
28 lines (23 loc) · 854 Bytes
/
server.js
File metadata and controls
28 lines (23 loc) · 854 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
26
27
28
const express = require('express');
const fs = require('fs');
const app = express();
const PORT = 4000;
// Function to read quotes from the JSON file
function loadQuotes() {
const data = fs.readFileSync('quotes.json', { encoding: 'utf8', flag: 'r' });
return JSON.parse(data);
}
const quotes = loadQuotes();
let currentQuoteIndex = 0; // Initial quote index
// Rotate the quote every 24 hours
setInterval(() => {
currentQuoteIndex = (currentQuoteIndex + 1) % quotes.length; // Cycle through the quotes
console.log(`Quote updated to: ${quotes[currentQuoteIndex].quote}`);
}, 86400000); // 86400000 ms = 24 hours
// Endpoint to get the current quote
app.get('/quote-of-the-day', (req, res) => {
res.json(quotes[currentQuoteIndex]);
});
app.listen(PORT, () => {
console.log(`Server running on http://localhost:${PORT}`);
});