You need to enable JavaScript to run this app.
导航

简单边缘动态/静态页面

最近更新时间2023.12.20 19:13:37

首次发布时间2023.02.15 19:04:51

以下示例展示了如何通过边缘函数回复一段静态或动态生成的http页面。

async function handleRequest(event) {
  // 获得用户的客户端ip
  const clientIp = event.info ? event.info.clientIp : "N/A";
  const request = event.request;
  const html = `<!DOCTYPE html>
    <body>
      <h1>Hello World</h1>
      <p>Hello from Sparrow Runtime.</p>
      <p>The visitor's ip is ${clientIp}</p>
    </body>`;
  return new Response(html, {
    headers: {
      "content-type": "text/html;charset=UTF-8",
    },
  });
}

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