Skip to content

Commit 3a5ed92

Browse files
committed
Add README.md
1 parent d2c39a6 commit 3a5ed92

1 file changed

Lines changed: 60 additions & 0 deletions

File tree

README.md

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# CDN
2+
3+
Cache Dango Web Solutions CMS data and built-in Soar CDN assets in your Node.js web app as JSON.
4+
5+
## Requirements
6+
7+
- Must be using Node.js v10.12 or higher.
8+
9+
## Usage
10+
11+
Include the `@dangoweb/cdn` package in your Express.js application:
12+
13+
```javascript
14+
const CDN = require('@dangoweb/cdn');
15+
const cdn = new CDN({
16+
dir: './cdn/',
17+
extension: '.cdn.json',
18+
ttl: 3600,
19+
});
20+
21+
async function cmsdata() { // Example fetching and caching CMS data
22+
if (cdn.valid('cms')) return cdn.get('cms'); // Return cached CMS data if valid
23+
try {
24+
return await fetch(`...`, { }) // Given fetch function to fetch CMS data from Dango Web Solutions
25+
.then(res => res.json())
26+
.then(res => {
27+
var cmsData = JSON.parse(JSON.stringify(res.data).replaceAll('.spaces', 'clients'));
28+
console.log('Writing...');
29+
cdn.cache('cms', cmsData); // Cache the CMS data
30+
return cmsData;
31+
});
32+
} catch {
33+
try {
34+
return cdn.get('cms'); // Fall back to cached CMS data
35+
} catch {
36+
console.error('Failed to fetch CMS data and no cached data found.');
37+
return null;
38+
};
39+
};
40+
};
41+
42+
async function startApp() {
43+
var cms = await cmsdata();
44+
console.log(cms);
45+
};
46+
47+
startApp();
48+
```
49+
50+
## Tests
51+
52+
Run tests using Jest:
53+
54+
```bash
55+
npm test
56+
```
57+
58+
## Credits
59+
60+
Original developed by [Komrod/local-cache](https://github.com/Komrod/local-cache)

0 commit comments

Comments
 (0)