返回JSON內容
更新時間 2023-12-04 20:31:29
最近更新時間: 2023-12-04 20:31:29
分享文章
本文介紹如何通過邊緣函數返回JSON內容。
直接響應邊緣函數代碼中的JSON內容。
示例代碼
async function handler(event) {
const data = {
hello: "BosonFaaS"
}
const json = JSON.stringify(data, null, 2)
return new Response(json, {
headers: {
"content-type": "application/json;charset=UTF-8"
}
})
}
addEventListener('fetch', (event) => {
event.respondWith(handler(event));
});
示例預覽
在瀏覽器地址欄中輸入匹配到HTTP路由的URL,即可預覽示例效果。
