KeyCDN is an awesome PAYG (pay-as-you-go) CDN service. I have been using them and I love them so far. I am planning on writing a more detailed post about why I like them and what makes them a great fit for us in hope to help someone to make a decision on which CDN service to use and just to give some props and praise to the guys behind keyCDN.
It is free to try and if you like it, you can get $5 off using this promo link
In this post I just wanted to show some code that interacts with their Node.js
library.
I am using a prompt
module to get input as to what needs to be done
The purgeZone(1)
and listZones(0)
are pretty much taken from the sample with some very minor enhancements to make the methods be more “output friendly”.
var secret = require('./secret.js'); //put your secret key here or you can use it directly passing to the keyCDN method.
var KeyCDN = require('keycdn');
var keycdn = new KeyCDN(secret.key);
function purgeZone(zoneID) {
// purge zone cache
keycdn.get('zones/purge/' + zoneID + '.json', function (err, res) {
console.log('GET zones/purge/' + zoneID + '.json');
if (err) {
console.log('ERR:' + 'Could not purge-> ' + zoneID);
console.trace(err);
} else {
console.log('ACK:' + 'Purge-> ' + zoneID);
console.dir(res);
}
});
}
function listZones() {
// get all zones
keycdn.get('zones.json', function (err, results) {
if (err) {
console.trace(err);
return;
}
console.dir(results);
console.log(results.data.zones);
});
}
console.log('-------------------------- START -------------------------------');
console.log("1. List Zones\n2. Purge Content Zones\n3. Purge All Zones\n\nChoose option: \n");
var ZONES1 = [
{
zone: 'name1',
id: 1111
},
{
zone: 'name2',
id: 1112
},
];
var ZONES2 = [
{
zone: 'name3',
id: 1113
}
];
var prompt = require('prompt');
prompt.start();
prompt.get(['insert'], function (err, result) {
var s = result.insert;
switch (s) {
case '1':
listZones();
break;
case '2':
ZONES1.forEach(function (zoneObj) {
purgeZone(zoneObj.id);
});
break;
case '3':
var allZones = ZONES1.concat(ZONES2);
allZones.forEach(function (zoneObj) {
purgeZone(zoneObj.id);
});
break;
default:
console.log("ERR: no action mathches the input");
}
});
the secret.js
is just one line
exports.key = '*****************************';
I could do a much nicer job I suppose using a commander
package or the optimist
but I just needed to throw something quick together to be able to basically purge all the zones or some types of zones quickly.