Cache Dango Web Solutions CMS data and built-in Soar CDN assets in your Node.js web app as JSON.
- Must be using Node.js v10.12 or higher.
Include the @dangoweb/cdn package in your Express.js application:
constCDN=require('@dangoweb/cdn');constcdn=newCDN({dir: './cdn/',extension: '.cdn.json',ttl: 3600,});asyncfunctioncmsdata(){// Example fetching and caching CMS dataif(cdn.valid('cms'))returncdn.get('cms');// Return cached CMS data if validtry{returnawaitfetch(`...`,{})// Given fetch function to fetch CMS data from Dango Web Solutions.then(res=>res.json()).then(res=>{varcmsData=JSON.parse(JSON.stringify(res.data).replaceAll('.spaces','clients'));console.log('Writing...');cdn.cache('cms',cmsData);// Cache the CMS datareturncmsData;});}catch{try{returncdn.get('cms');// Fall back to cached CMS data}catch{console.error('Failed to fetch CMS data and no cached data found.');returnnull;};};};asyncfunctionstartApp(){varcms=awaitcmsdata();console.log(cms);};startApp();Run tests using Jest:
npm testOriginal developed by Komrod/local-cache