【问题标题】:how to render a html page using cloudflare workers如何使用 cloudflare 工作者渲染 html 页面
【发布时间】:2020-10-21 09:49:28
【问题描述】:

我创建了一个路由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',
      },
    })
  }
}

【问题讨论】:

    标签: javascript html cloudflare worker


    【解决方案1】:

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

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

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-03-17
        • 1970-01-01
        • 2010-11-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多