Created
July 24, 2021 21:25
-
-
Save supercede/770402ca2bdb1732608fff8fc9ca2850 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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