diff options
Diffstat (limited to 'btcdashboard/server3.js')
| -rw-r--r-- | btcdashboard/server3.js | 43 |
1 files changed, 43 insertions, 0 deletions
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 @@ | |||
| 1 | const express = require('express'); | ||
| 2 | const cors = require('cors'); | ||
| 3 | const redis = require('redis'); | ||
| 4 | |||
| 5 | const app = express(); | ||
| 6 | const PORT = 3001; | ||
| 7 | |||
| 8 | const redisClient = redis.createClient({ | ||
| 9 | url: 'redis://localhost:6379' // this is default port | ||
| 10 | }); | ||
| 11 | |||
| 12 | // CORS | ||
| 13 | app.use((req, res, next) => { | ||
| 14 | res.header('Access-Control-Allow-Origin', '*'); | ||
| 15 | res.header('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE'); | ||
| 16 | res.header('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept'); | ||
| 17 | next(); | ||
| 18 | }); | ||
| 19 | |||
| 20 | app.get('/api/prices', async (req, res) => { | ||
| 21 | try { | ||
| 22 | const prices = await redisClient.lRange('btc_prices', 0, -1); | ||
| 23 | |||
| 24 | res.header('Access-Control-Allow-Origin', '*'); | ||
| 25 | res.header('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE'); | ||
| 26 | res.header('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept'); | ||
| 27 | |||
| 28 | res.json(prices); | ||
| 29 | } catch (error) { | ||
| 30 | console.error('Error fetching prices:', error); | ||
| 31 | res.status(500).json({ error: 'Internal Server Error' }); | ||
| 32 | } | ||
| 33 | }); | ||
| 34 | |||
| 35 | async function startServer() { | ||
| 36 | await redisClient.connect(); | ||
| 37 | |||
| 38 | app.listen(PORT, () => { | ||
| 39 | console.log(`Server running on http://localhost:${PORT}`); | ||
| 40 | }); | ||
| 41 | } | ||
| 42 | |||
| 43 | startServer().catch(console.error); | ||
