emptygif
ภาษา: ไทย

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

Firebase Cloud Functions + Hosting JavaScript

ตัวอย่างโค้ด JavaScript สำหรับ Firebase Cloud Functions + Hosting ที่ตอบคำขอไปยัง /pixel.gif ด้วย GIF ว่าง ซึ่งถอดรหัสเพียงครั้งเดียวตอนเริ่มทำงาน

Firebase Cloud Functions + Hosting JavaScript

ติดตั้ง
npm i firebase-functions firebase-admin
ไฟล์
index.js
รัน
firebase emulators:start
// functions/index.js - and in firebase.json:
//   "hosting": { "rewrites": [{ "source": "/pixel.gif", "function": { "functionId": "pixel" } }] }
import { onRequest } from 'firebase-functions/https';

const GIF = Buffer.from('R0lGODlhAQABAIAAAP///wAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw==', 'base64');

export const pixel = onRequest((req, res) => {
  res.set('Cache-Control', 'no-store').type('gif').send(GIF);
});

เราคอมไพล์หรือตรวจชนิดข้อมูลตัวอย่างโค้ดนี้แล้ว

ตรวจสอบ

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

เว็บไซต์ทางการ: Firebase Cloud Functions + Hosting

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