Created
April 1, 2021 21:43
-
-
Save ardeay/4fa87a660e42fa803cf94854788248db to your computer and use it in GitHub Desktop.
NodeJS Function to Purge Fastly CDN by Zesty ZUID
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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