繁体   English   中英

如何使用 cloudflare 工作人员呈现 html 页面

[英]how to render a html page using cloudflare workers

我创建了一个路由https://bewithrits.tech ,当我使用https://bewithrits.tech/ 时,它将呈现所需的页面,但是当我添加https://bewithrits.tech/ 时 https://bewithrits.tech/?fb然后它会给出错误 Error 1000 DNS 指向要在给定路由中实现的禁止 IP 不想使用https://bewithrits.tech/* 这是我尝试过的代码

const html1 = `<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <link
      href="https://unpkg.com/material-components-web@latest/dist/material-components-web.min.css"
      rel="stylesheet"
    />
    <script src="https://unpkg.com/material-components-web@latest/dist/material-components-web.min.js"></script>
    <link
      rel="stylesheet"
      href="https://fonts.googleapis.com/icon?family=Material+Icons"
    />
    <style>
      .button {
        background-color: rgba(162, 51, 79, 0.3);
        text-decoration: none;
        color: #a2334f;
        width: 150px;
        border: none;
        padding: 13px 26px;
        text-align: center;
        display: inline-block;
        font-weight: 600;
        font-size: 16px;
        line-height: 19px;
        margin: 4px 2px;
        cursor: pointer;
        opacity: 0.7;
        border-radius: 5px;
      }
    </style>
  </head>
  <body>
    <a href="" class="button"> Know More </a>
  </body>
</html>
`


const html2 = `<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <link
      href="https://unpkg.com/material-components-web@latest/dist/material-components-web.min.css"
      rel="stylesheet"
    />
    <script src="https://unpkg.com/material-components-web@latest/dist/material-components-web.min.js"></script>
    <link
      rel="stylesheet"
      href="https://fonts.googleapis.com/icon?family=Material+Icons"
    />
    <style>
      .button {
        background-color: rgba(162, 51, 79, 0.3);
        text-decoration: none;
        color: #a2334f;
        width: 150px;
        border: none;
        padding: 13px 26px;
        text-align: center;
        display: inline-block;
        font-weight: 600;
        font-size: 16px;
        line-height: 19px;
        margin: 4px 2px;
        cursor: pointer;
        opacity: 0.7;
        border-radius: 5px;
      }
    </style>
  </head>
  <body>
    <a href="" class="button"> Know More </a>

    <p>queryy</p>
  </body>
</html>
`
addEventListener('fetch', (event) => {
  event.respondWith(handleRequest(event.request))
})

async function handleRequest(request) {
 console.log(request.url)
 console.log(request.url.includes('/?'))

  if(request.url==='https://bewithrits.tech' ||request.url==='https://bewithrits.tech/'){
 return new Response(html1, {
      headers: {
        'content-type': 'text/html;charset=UTF-8',
        'Access-Control-Allow-Origin': 'https://bewithrits.tech',
        'Access-Control-Allow-Methods': 'OPTIONS, HEAD, POST, GET',
        'Access-Control-Allow-Headers':
          'X-Requested-With, Authorization,' + 'Content-Type, Accept',
      },
    })

  }
  
  else if(request.url.includes('/?')){
     return new Response(html2, {
      headers: {
        'content-type': 'text/html;charset=UTF-8',
        'Access-Control-Allow-Origin': 'https://bewithrits.tech',
        'Access-Control-Allow-Methods': 'OPTIONS, HEAD, POST, GET',
        'Access-Control-Allow-Headers':
          'X-Requested-With, Authorization,' + 'Content-Type, Accept',
      },
    })
  }
}

您需要使用您的网络服务器 IP 地址作为 A 记录。 您将 A 记录指向 Cloudflare 的服务器,您不应该这样做。

https://support.cloudflare.com/hc/en-us/articles/360029779472-Troubleshooting-Cloudflare-1XXX-errors#error1000

如果要将 URL 与查询参数(其中带有? URL)匹配,则需要尾随* 不幸的是,没有办法解决这个问题。

这是 Cloudflare Workers 路由模式现在解释方式的一个已知问题。 我们想改变它,但这很棘手,因为我们不想破坏任何依赖于现有行为的现有工人。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM