10分钟使用cloudflare免费搭建支持nodejs、键值对及静态文件存储的免备案网站

10分钟使用cloudflare免费搭建支持nodejs、键值对及静态文件存储的免备案网站

国内的网站都需要域名的备案,一个备案短则7天,长则一个月时间,太麻烦了,如果使用香港或国外空间,又需要支付费用,今天我来介绍一个免费搭建静态及动态免备案的方法。

10分钟使用cloudflare免费搭建支持nodejs、键值对及静态文件存储的免备案网站

一、创建静态托管网站

注册网址:https://dash.cloudflare.com/

创建静态托管网站

注册登录后点击左侧的pages,点击右侧创建项目按钮

10分钟使用cloudflare免费搭建支持nodejs、键值对及静态文件存储的免备案网站

支持git项目下载、自己上传静态文件和动态构建

10分钟使用cloudflare免费搭建支持nodejs、键值对及静态文件存储的免备案网站

我们选择直接上传

10分钟使用cloudflare免费搭建支持nodejs、键值对及静态文件存储的免备案网站

为项目创建一个唯一的名字

10分钟使用cloudflare免费搭建支持nodejs、键值对及静态文件存储的免备案网站

上传本地文件夹或压缩包即可

10分钟使用cloudflare免费搭建支持nodejs、键值对及静态文件存储的免备案网站

上传完成后既可以看到访问地址:

10分钟使用cloudflare免费搭建支持nodejs、键值对及静态文件存储的免备案网站

还可以自定义域名,先要注册域名,然后将域名的dns改成cloudflare的就行了,这样也不需要备案。

10分钟使用cloudflare免费搭建支持nodejs、键值对及静态文件存储的免备案网站

二、动态网站

如果我们要做一个动态网站,我们可以使用worker,代码支持ES Modules与Service Workers写法:

10分钟使用cloudflare免费搭建支持nodejs、键值对及静态文件存储的免备案网站

创建worker后,我们再创建一个kv键值对存储空间。

10分钟使用cloudflare免费搭建支持nodejs、键值对及静态文件存储的免备案网站

10分钟使用cloudflare免费搭建支持nodejs、键值对及静态文件存储的免备案网站

点击创建命名空间,输入一个名字,例如mydata

10分钟使用cloudflare免费搭建支持nodejs、键值对及静态文件存储的免备案网站

添加完后还要在worker里点击设置-》变量-》kv命名空间绑定,输入刚才的名字mydata

10分钟使用cloudflare免费搭建支持nodejs、键值对及静态文件存储的免备案网站

10分钟使用cloudflare免费搭建支持nodejs、键值对及静态文件存储的免备案网站

绑定完成后我们可以在worker的动态代码中进行访问和存储查询

addEventListener("fetch", (event) => {
  event.respondWith(handleRequest(event.request));
});

async function handleRequest(request) {

  
  const value = await mydata.get("first-key");
  if (value === null) {
    await mydata.put('first-key',+new Date())
    return new Response("Value not found", { status: 404 });
  }

  return new Response(value);
}

10分钟使用cloudflare免费搭建支持nodejs、键值对及静态文件存储的免备案网站

更高阶的功能可以参考官网文档:https://developers.cloudflare.com/workers/runtime-apis/kv#kv-bindings

当然worker还可以实现代理请求,domain_list换成当前你运行的worker的二级域名地址,upstream就是你想镜像访问的地址。

// List of domains bind to your WorkersProxy.
const domain_list = ['https://google.error.workers.dev']

// Website you intended to retrieve for users.
const upstream = 'https://www.google.com/'

// Website you intended to retrieve for users using mobile devices.
const upstream_mobile = 'https://www.google.com/'

// Countries and regions where you wish to suspend your service.
const blocked_region = ['KP', 'SY', 'PK', 'CU']

// IP addresses which you wish to block from using your service.
const blocked_ip_address = ['0.0.0.0', '127.0.0.1']


addEventListener('fetch', event => {
    event.respondWith(fetchAndApply(event.request));
})

async function fetchAndApply(request) {

    const region = request.headers.get('cf-ipcountry').toUpperCase();
    const ip_address = request.headers.get('cf-connecting-ip');
    const user_agent = request.headers.get('user-agent');
    const http = "http://";
    const https = "https://";
    
    let response = null;
    let url = request.url;

    if (url.startsWith(http)) {
        url = url.replace(http, https);
        response = Response.redirect(url);
        return response;
    }

    if (await device_status(user_agent)) {
        upstream_domain = upstream
    } else {
        upstream_domain = upstream_mobile
    }

    for(let domain of domain_list) {
        url = url.replace(domain, upstream_domain)
    };

    if (blocked_region.includes(region)) {
        response = new Response('Access denied: WorkersProxy is not available in your region yet.', {
            status: 403
        });
    } else if(blocked_ip_address.includes(ip_address)){
        response = new Response('Access denied: Your IP address is blocked by WorkersProxy.', {
            status: 403
        });
    } else{
        let method = request.method;
        let request_headers = request.headers;
        let new_request_headers = new Headers(request_headers)
        let host_name = upstream_domain.replace(http, '')
        host_name = upstream_domain.replace(https, '')
        host_name = upstream_domain.replace('/', '')

        new_request_headers.set('Host', host_name)
        new_request_headers.set('Referer', upstream_domain)

        origin_response = await fetch(url, {
            method: method,
            headers: new_request_headers
        })
        
        let response_body = origin_response.body
        let response_headers = origin_response.headers
        let new_response_headers = new Headers(response_headers)
        let status = origin_response.status
        
        new_response_headers.set('access-control-allow-origin', '*');
        new_response_headers.set('access-control-allow-credentials', true);
        new_response_headers.delete('content-security-policy');
        new_response_headers.delete('content-security-policy-report-only');
        new_response_headers.delete('clear-site-data');

        response = new Response(response_body, {
            status,
            headers: response_headers
        })
    }
    return response;
}

async function device_status (userAgentInfo) {
    var Agents = ["Android", "iPhone", "SymbianOS", "Windows Phone", "iPad", "iPod"];
    var flag = true;
    for (var v = 0; v < Agents.length; v++) {
        if (userAgentInfo.indexOf(Agents[v]) > 0) {
            flag = false;
            break;
        }
    }
    return flag;
}

{{collectdata}}

网友评论0