使用边缘函数生成 JSON,并在浏览器端访问预览该 JSON。
一、示例代码
const data = { content: 'hello world',};async function handleRequest(request) { // JSON 转为字符串 const result = JSON.stringify(data, null, 2); return new Response(result, { headers: { 'content-type': 'application/json; charset=UTF-8', }, });}addEventListener('fetch', event => { return event.respondWith(handleRequest(event.request));});
二、示例预览
在浏览器地址栏中输入匹配到边缘函数触发规则的 URL,即可预览到示例效果。
三、相关参考
·Runtime APIs: addEventListener
·Runtime APIs: Response