以编程方式清除 cloudflare 缓存
在将请求发送到 node.js api 后,我试图以编程方式清除单个 url 的 cloudflare 缓存.我正在使用 https://github.com/cloudflare/node-cloudflare 库,但是我无法弄清楚如何记录来自 cloudflare 的回调.根据同一个 repo 中的测试文件,语法应该是这样的:
I am trying to clear the cloudflare cache for single urls programmatically after put requests to a node.js api. I am using the https://github.com/cloudflare/node-cloudflare library, however I can't figure out how to log a callback from cloudflare. According to the test file in the same repo, the syntax should be something like this:
//client declaration:
t.context.cf = new CF({
key: 'deadbeef',
email: 'cloudflare@example.com',
h2: false
});
//invoke clearCache:
t.context.cf.deleteCache('1', {
files: [
'https://example.com/purge_url'
]
})
我如何从这个请求中读出回调?我在自己的代码中尝试了以下内容:
How can I read out the callback from this request? I have tried the following in my own code:
client.deleteCache(process.env.CLOUDFLARE_ZONE, { "files": [url] }, function (data) {
console.log(`Cloudflare cache purged for: ${url}`);
console.log(`Callback:${data}`);
})
和:
client.deleteCache('1', {
files: [
'https://example.com/purge_url'
]
}).then(function(a,b){
console.log('helllllllooooooooo');
})
无济于事.:(
通过 url 清除 Cloudflare 缓存:
Purging Cloudflare cache by url:
var Cloudflare = require('cloudflare');
const { CF_EMAIL, CF_KEY, CF_ZONE } = process.env;
if (!CF_ZONE || !CF_EMAIL || !CF_KEY) {
throw new Error('you must provide env. variables: [CF_ZONE, CF_EMAIL, CF_KEY]');
}
const client = new Cloudflare({email: CF_EMAIL, key: CF_KEY});
const targetUrl = `https://example.com/purge_url`;
client.zones.purgeCache(CF_ZONE, { "files": [targetUrl] }).then(function (data) {
console.log(`Cloudflare cache purged for: ${targetUrl}`);
console.log(`Callback:`, data);
}, function (error) {
console.error(error);
});
您可以通过这种方式查找 cloudflare 区域:
You can lookup cloudflare zone this way:
client.zones.browse().then(function (zones) {
console.log(zones);
})
不要忘记安装当前的客户端版本:
Don't forget to install the current client version:
npm i cloudflare@^2.4.1 --save-dev