以编程方式清除cloudflare缓存 [英] clearing cloudflare cache programmatically

查看:519
本文介绍了以编程方式清除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

这篇关于以编程方式清除cloudflare缓存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆