Created
September 16, 2018 12:57
-
-
Save iraniamir/447fbc2178ea4273a74625248373624a to your computer and use it in GitHub Desktop.
CacheRestServer
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
const express = require('express'); | |
const axios = require('axios'); | |
const cacheAdapter = require('axios-cache-adapter'); | |
const app = express() | |
const port = 3210 | |
const fetchCache = cacheAdapter.setup({ | |
cache: { | |
maxAge: 15 * 60 * 1000 | |
} | |
}) | |
async function fetch(url, method, data, headers) { | |
const request = { | |
method, | |
url, | |
data, | |
headers, | |
timeout: 250000, // 250s | |
}; | |
try { | |
const response = await fetchCache(request); | |
return response.data; | |
} catch (error) { | |
console.log("ERR", error); | |
} | |
} | |
app.use('/:path*', async function (req, res) { | |
const fetchRes = await fetch(req.originalUrl.substring(1), req.method, req.params, req.headers); | |
res.send(fetchRes) | |
}); | |
app.listen(port, () => console.log(`Example app listening on port ${port}!`)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment