const express = require('express');
const Redis = require('ioredis');
const path = require('path');
const redisUri = process.env.REDIS_URI;
const redis = new Redis(redisUri);
redis.on('connect', () => {
console.log('Connected to Redis');
});
redis.on('error', (err) => {
console.error('Redis connection error:', err);
});
const app = express();
app.set('view engine', 'ejs');
app.set('views', path.join(__dirname, 'views'));
app.use(express.urlencoded({ extended: true }));
app.get('/', (req, res) => {
res.render('index', { connected: redis.status === 'ready' });
});
app.post('/store', async (req, res) => {
const { key, value } = req.body;
try {
await redis.set(key, value);
res.redirect('/');
} catch (err) {
console.error('Error storing data in Redis:', err);
res.status(500).send('Error storing data in Redis');
}
});
app.post('/retrieve', async (req, res) => {
const { key } = req.body;
try {
const value = await redis.get(key);
res.render('index', { connected: redis.status === 'ready', retrievedData: value });
} catch (err) {
console.error('Error retrieving data from Redis:', err);
res.status(500).send('Error retrieving data from Redis');
}
});
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
console.log(`server is running on port ${PORT}`);
});