Cloudflare Pages Functions TypeScript
ตัวอย่างโค้ด TypeScript สำหรับ Cloudflare Pages Functions ที่ตอบคำขอไปยัง /pixel.gif ด้วย GIF ว่าง ซึ่งถอดรหัสเพียงครั้งเดียวตอนเริ่มทำงาน
Cloudflare Pages Functions TypeScript
// functions/pixel.gif.ts (the file path is the route)
const GIF = Uint8Array.from(atob('R0lGODlhAQABAIAAAP///wAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw=='), (c) => c.charCodeAt(0));
export const onRequestGet: PagesFunction = () =>
new Response(GIF, {
headers: { 'Content-Type': 'image/gif', 'Cache-Control': 'no-store' },
});
เรารันตัวอย่างโค้ดนี้และเทียบไบต์ที่ได้กลับมาแล้ว
ตรวจสอบ
curl -sI http://localhost:8080/pixel.gif
เว็บไซต์ทางการ: Cloudflare Pages Functions