以编程方式清除cloudflare缓存

以编程方式清除cloudflare缓存

问题描述:

在将请求发送到node.js api之后,我试图以编程方式清除单个网址的cloudflare缓存.我正在使用 https://github.com/cloudflare/node-cloudflare 库,但是我无法弄清楚如何从cloudflare记录回调.根据同一仓库中的测试文件,语法应如下所示:

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');
})

无济于事. :(

我编写了一个nodejs模块来清除整个网站的缓存.它扫描您的公共"文件夹,构建完整的URL,然后在cloudflare上清除它:

I wrote a nodejs module to purge cache for a entire website. It scan your "public" folder, build the full url and purge it on cloudflare:

您可以使用npx运行它:

You can run it using npx:

npm install -g npx

npx purge-cloudflare-cache your@email.com your_cloudflare_key the_domain_zone https://your.website.com your/public/folder

但是,您也可以安装它并使用npm运行:

But, you can install it and run using npm too:

npm install -g purge-cloudflare-cache

purge your@email.com your_cloudflare_key the_domain_zone https://your.website.com your/public/folder

对于像这样的公共/文件夹树:

For a public/folder tree like:

├── assets
│   ├── fonts
│   │   ├── roboto-regular.ttf
│   │   └── roboto.scss
│   ├── icon
│   │   └── favicon.ico
│   └── imgs
│       └── logo.png
├── build
│   ├── main.css
│   ├── main.js
├── index.html

它将清除文件的缓存:

https://your.website.com/index.html
https://your.website.com/build/main.css
https://your.website.com/build/main.js
https://your.website.com/assets/imgs/logo.png
https://your.website.com/assets/icon/favicon.ico
https://your.website.com/assets/fonts/roboto.css
https://your.website.com/assets/fonts/roboto-regular.ttf