Node.js module to perform requests and caching in json files for the next request.
npm i cjr
const cjr = require('cjr');
cjr.fetch('http://echo.jsontest.com/user/JappaBR')
.then(res => console.log(res))
.catch(console.error);
// { user: 'JappaBR' }
In the next requests, the server will not be requested, it will return the value stored in json.
// Request response time comparison
const cjr = require('./main.js');
const options = { isCached: true };
async function run(counter) {
console.time(counter);
const res = await cjr.fetch('http://echo.jsontest.com/user/JappaBR', options);
console.timeEnd(counter);
console.log(res);
};
(async() => {
for(let i=0; i<3; i++) await run(i)
})();
// 0: 424ms
// { cached: false, data: { user: 'JappaBR' } }
// 1: 0.82ms
// { cached: true, data: { user: 'JappaBR' } }
// 2: 0.79ms
// { cached: true, data: { user: 'JappaBR' } }
isCached(boolean)
[false
] - If true, will return along with the response whether the value from cache or not.
timeout(number)
[30
] - The timeout (in seconds) to ignore cache and fetch again.
dir`` - The channel id where you write messages to.
dir(string)
[`./cache`]` - Folder path where json files will be stored. The default value is on 'cache' folder in module root.
Contributions are welcome! Please feel free to open an issue or submit a pull request, for bug fixes or new features.
- Fork the repository
- Create a new branch
git checkout -b <new-feature-name>
- Make the changes
- Commit the changes
git commit -am "Add new feature"
- Push the changes
git push origin <new-feature-name>
- Create a pull request on GitHub
Many thanks!