emptygif
ภาษา: ไทย

ภาษาและเฟรมเวิร์กทั้งหมด

Deno Deploy (deno serve) TypeScript

ตัวอย่างโค้ด TypeScript สำหรับ Deno Deploy (deno serve) ที่ตอบคำขอไปยัง /pixel.gif ด้วย GIF ว่าง ซึ่งถอดรหัสเพียงครั้งเดียวตอนเริ่มทำงาน

Deno Deploy (deno serve) TypeScript

ไฟล์
main.ts
รัน
deno serve --port 8080 main.ts
import { decodeBase64 } from 'jsr:@std/encoding/base64';

const GIF = decodeBase64('R0lGODlhAQABAIAAAP///wAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw==');

export default {
  fetch(req) {
    if (new URL(req.url).pathname !== '/pixel.gif') {
      return new Response('Not Found', { status: 404 });
    }
    return new Response(GIF, {
      headers: { 'Content-Type': 'image/gif', 'Cache-Control': 'no-store' },
    });
  },
} satisfies Deno.ServeDefaultExport;

เรารันตัวอย่างโค้ดนี้และเทียบไบต์ที่ได้กลับมาแล้ว

ตรวจสอบ

curl -sI http://localhost:8080/pixel.gif

เว็บไซต์ทางการ: Deno Deploy (deno serve)

เพิ่มเติมใน TypeScript