This web app retrieves images from the web, specifically favicons. So at every refresh of the page, a request is made to the website, and the icon is fetched again.
This is the function to which I pass an URL ; favicon()
is a library that scraps it from the web site / page, and returns it.
router.get('/feedicon', function(req, res) {
favicon(req.query.url, function(err, icon) {
if (icon) {
res.send(icon);
} else {
res.status(500).send('No icon found');
}
});
});
Now if a valid icon
file is found, it would be nice to
/cache
directory exists / create it (or return an error if it's not possible) ;icon
is in the /cache
directory ;I'd rather not install a lib to do that, in fact where I'm stumbling is what would be the best way to "uniquely timestamp" the cached images, so as to ensure to serve the right file..?
Also I'm worrying about performances if I use a hash to id the file..?
What are the best practices for quickly caching a file like this? I'm having a hard time searching for it, it's almost like nobody needs to cache dynamic images in NodeJs / express..?
Here a simple example caching the folder public
where i store the images
const cacheTime = 86400000 * 30 // the time you want
const path = require('path')
.
.
.
.
app.use(express.static(path.join(__dirname, 'public'), {
maxAge: cacheTime
}))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With