Skip to content

Instantly share code, notes, and snippets.

@supercede
Created July 24, 2021 21:25
Show Gist options
  • Save supercede/770402ca2bdb1732608fff8fc9ca2850 to your computer and use it in GitHub Desktop.
Save supercede/770402ca2bdb1732608fff8fc9ca2850 to your computer and use it in GitHub Desktop.
const express = require('express');
const axios = require('axios').default;
const { client, getAsync } = require('./redis');
const app = express();
app.get('/countries', async (request, response) => {
try {
const redisKey = 'COUNTRIES';
const countries = await getAsync(redisKey);
if (countries) {
const data = JSON.parse(countries);
return response.status(200).json({ countries: data });
}
const { data } = await axios.get('https://restcountries.eu/rest/v2/all');
await client.set(redisKey, JSON.stringify(data));
return response.status(200).json({ countries: data });
} catch (error) {
console.log(error);
return response.status(500).json({
status: 'error',
error: error.message,
});
}
});
app.listen(3000, () => {
console.log('server listening on port 3000');
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment