From fabefacd8da4932c9a5e8b4aec33d196c290d33b Mon Sep 17 00:00:00 2001 From: hc Date: Thu, 12 Sep 2024 11:46:51 +0800 Subject: archive of tuffy and btcdashboard --- btcdashboard/server3.js | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 btcdashboard/server3.js (limited to 'btcdashboard/server3.js') diff --git a/btcdashboard/server3.js b/btcdashboard/server3.js new file mode 100644 index 0000000..72bfb8d --- /dev/null +++ b/btcdashboard/server3.js @@ -0,0 +1,43 @@ +const express = require('express'); +const cors = require('cors'); +const redis = require('redis'); + +const app = express(); +const PORT = 3001; + +const redisClient = redis.createClient({ + url: 'redis://localhost:6379' // this is default port +}); + +// CORS +app.use((req, res, next) => { + res.header('Access-Control-Allow-Origin', '*'); + res.header('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE'); + res.header('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept'); + next(); +}); + +app.get('/api/prices', async (req, res) => { + try { + const prices = await redisClient.lRange('btc_prices', 0, -1); + + res.header('Access-Control-Allow-Origin', '*'); + res.header('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE'); + res.header('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept'); + + res.json(prices); + } catch (error) { + console.error('Error fetching prices:', error); + res.status(500).json({ error: 'Internal Server Error' }); + } +}); + +async function startServer() { + await redisClient.connect(); + + app.listen(PORT, () => { + console.log(`Server running on http://localhost:${PORT}`); + }); +} + +startServer().catch(console.error); -- cgit v1.2.3-70-g09d2