emptygif
ภาษา: ไทย

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

srvx (universal fetch server) TypeScript

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

srvx (universal fetch server) TypeScript

ติดตั้ง
npm i srvx
ไฟล์
server.ts
รัน
npx srvx server.ts
import { serve } from 'srvx';

const GIF = Uint8Array.from(atob('R0lGODlhAQABAIAAAP///wAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw=='), (c) => c.charCodeAt(0));

// The same code runs on Node.js, Deno and Bun.
serve({
  port: 8080,
  fetch(request) {
    if (new URL(request.url).pathname !== '/pixel.gif') {
      return new Response('Not Found', { status: 404 });
    }
    return new Response(GIF, {
      headers: {
        'Content-Type': 'image/gif',
        'Content-Length': String(GIF.length),
        'Cache-Control': 'no-store',
      },
    });
  },
});

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

ตรวจสอบ

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

เว็บไซต์ทางการ: srvx (universal fetch server)

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