Skip to content

Instantly share code, notes, and snippets.

@ardeay
Created April 1, 2021 21:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ardeay/4fa87a660e42fa803cf94854788248db to your computer and use it in GitHub Desktop.
Save ardeay/4fa87a660e42fa803cf94854788248db to your computer and use it in GitHub Desktop.
NodeJS Function to Purge Fastly CDN by Zesty ZUID
var corsOptions = {
origin: function(origin, callback) {
if (whitelist.indexOf(origin) !== -1) {
callback(null, true);
} else {
callback(new Error("Not allowed by CORS"));
}
}
};
require('env-yaml').config({ path: '.env.yaml' })
exports.fastlyPurge = (req, res) => {
const cors = require("cors")();
cors(req, res, () => {
exportFastlyPurge(req, res);
});
};
const exportFastlyPurge = async (req, res) => {
// error handling
if (req.method !== "GET") {
return res.status(400).send("Error: expected HTTP GET.");
}
let cacheKey = req.query.hasOwnProperty("zuid") ? req.query.zuid : false;
if(!cacheKey){
return res.status(400).send("Error: expected query param 'zuid'. Add ?zuid=7-XXXXX-XXXXXXX to the end of this url");
}
var fastly = require('fastly')(process.env.FASTLY_KEY);
fastly.purgeKey(process.env.FASTLY_SERVICE, cacheKey, function (err, obj) {
if (err) return res.send(err) // Oh no!
res.send(obj); // Response body from the fastly API
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment