Skip to content

Instantly share code, notes, and snippets.

@iraniamir
Created September 16, 2018 12:57
Show Gist options
  • Save iraniamir/447fbc2178ea4273a74625248373624a to your computer and use it in GitHub Desktop.
Save iraniamir/447fbc2178ea4273a74625248373624a to your computer and use it in GitHub Desktop.
CacheRestServer
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