يقرأ هذا المقطع ملفًا من القرص باستخدام Bun.file(). يعيد هذا مثيل BunFile، والذي يمكن تمريره مباشرة إلى منشئ new Response.
ts
const path = "/path/to/file.txt";
const file = Bun.file(path);
const resp = new Response(file);يتم قراءة Content-Type من الملف وتعيينه تلقائيًا على Response.
ts
new Response(Bun.file("./package.json")).headers.get("Content-Type");
// => application/json;charset=utf-8
new Response(Bun.file("./test.txt")).headers.get("Content-Type");
// => text/plain;charset=utf-8
new Response(Bun.file("./index.tsx")).headers.get("Content-Type");
// => text/javascript;charset=utf-8
new Response(Bun.file("./img.png")).headers.get("Content-Type");
// => image/pngنجمع كل هذا معًا مع Bun.serve().
ts
// خادم الملفات الثابتة
Bun.serve({
async fetch(req) {
const path = new URL(req.url).pathname;
const file = Bun.file(path);
return new Response(file);
},
});راجع الوثائق > API > ملف I/O للحصول على الوثائق الكاملة لـ Bun.write().