Created
March 10, 2019 01:27
-
-
Save bouiboui/f7e2cfa5485f748df705841b687c3ad0 to your computer and use it in GitHub Desktop.
This file contains 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
require('dotenv').load(); | |
const db = require('../lib/db'); | |
const md5 = require('../lib/md5'); | |
const fetchAmazonSearchResults = require('../lib/fetch-amazon-search-results'); | |
const amazon = { | |
updateSearchResults: async query => { | |
console.log(`Fetching results for query: ${query}`); | |
const results = await fetchAmazonSearchResults(query); | |
results && console.log(`Found ${results.length} results for query: ${query}`); | |
await db.set('amazon.results.' + md5(query), results); | |
return results; | |
}, | |
getSearchResults: async query => { | |
const results = await db.get('amazon.results.' + md5(query)); | |
results && console.log(`Found ${results.length} results for query: ${query}`); | |
if (results) return results; | |
return await amazon.updateSearchResults(query); | |
}, | |
}; | |
module.exports = async (req, res) => { | |
const [, query] = req.params; | |
const results = await amazon.getSearchResults(query); | |
return res.json(results); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment