
Security News
Deno 2.4 Brings Back deno bundle, Improves Dependency Management and Observability
Deno 2.4 brings back bundling, improves dependency updates and telemetry, and makes the runtime more practical for real-world JavaScript projects.
request-cached
Advanced tools
This library adds to request library the ability to cache data retrieved from a main server into a local server.
This library adds to request library the ability to cache data retrieved from a main server into a local server.
So, when you are requesting data for the first time, it will be retrieved from the main server and saved locally. Afterwards whenever you do the request for a second time, the data is retrieved locally.
Useful when scrapping.
Install request-cached
with npm.
npm install request-cached --save
In order to use request-cached
you need to provide the request
object you want to use.
const request = require('request'),
requestCached = require('request-cached')(request);
return requestCached(params)
.then((result) => {
// Do something with the result
})
.catch((error) => {
// Do something with the error
});
The request
object can be any other request-compatible object like throttled-request
. You can even provide a different object for the cache and for the main requests, as you could probably have some limitations for the requests to the main server which you don't for the local server.
const cacheRequest = require('request'),
mainRequest = require('throttled-request')(cacheRequest),
requestCached = require('request-cached')(mainRequest, cacheRequest);
mainRequest.configure({
requests: 2,
milliseconds: 1000
});
return requestCached(params)
.then((result) => {
// Do something with the result
})
.catch((error) => {
// Do something with the error
});
main
: Compulsory object where are set the params to access online data
cache
: Optional object where are set the params to access cached data
save
: Optional object where are set the params to save online data
path
: Path to save data toparseFn
: Function used to modify data before saving itrequest
: Optional object where are set the additional params to access online and cached data
customErrorFn
: Custom function to determine when a response is considered an error. By default:customErrorFn: (error, response, body) => error
Result
parameter if resolvedObject with the following keys:
response
: Second argument of a request callbackbody
: Third argument of a request callbackisCached
: A boolean indicating whether the data is retrieved from cache or not.Error
parameter if rejectedIt is the first argument of a request callback
const request = require('request'),
requestCached = require('request-cached')(request);
function getParams(userId) {
return {
main: {
uri: 'http://real/data/domain/mainUrl',
qs: {
id: userId
}
},
cache: {
uri: `http://localhost/cachedUrl/${userId}`
},
save: {
path: `/var/www/cachedUrl/${userId}`
},
request: {
proxy: 'http://localhost:8080',
encoding: 'utf8'
}
};
}
var userId = 12345;
return requestCached(getParams(userId))
.then((result) => {
console.log(result.body);
})
.catch((error) => {
console.error(error);
});
If you wanna test the code, follow these steps:
> git clone https://github.com/germanrio/request-cached.git
> cd request-cached
> npm install
> npm test
Note: Of course is also needed to have properly installed node
and npm
.
If you also want to see the code coverage of tests:
> npm run cov
It will generate a coverage
folder with the coverage report.
MIT
More details in the LICENSE
file in root folder.
FAQs
This library adds to request library the ability to cache data retrieved from a main server into a local server.
We found that request-cached demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Deno 2.4 brings back bundling, improves dependency updates and telemetry, and makes the runtime more practical for real-world JavaScript projects.
Security News
CVEForecast.org uses machine learning to project a record-breaking surge in vulnerability disclosures in 2025.
Security News
Browserslist-rs now uses static data to reduce binary size by over 1MB, improving memory use and performance for Rust-based frontend tools.