emptygif
言語: 日本語

すべての言語・フレームワーク

Encore.ts TypeScript

/pixel.gif へのリクエストに空の GIF を返す、Encore.ts 用の TypeScript スニペット。GIF は起動時に一度だけデコードします。

Encore.ts TypeScript

インストール
encore app create --example=ts/hello-world
ファイル
pixel.ts
実行
encore run
// pixel/pixel.ts  (inside a service directory with an encore.service.ts)
import { api } from 'encore.dev/api';

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

// A raw endpoint gets Node's req/res, so the bytes are written untouched.
export const pixel = api.raw(
  { expose: true, method: 'GET', path: '/pixel.gif' },
  async (req, resp) => {
    resp.writeHead(200, {
      'Content-Type': 'image/gif',
      'Content-Length': GIF.length,
      'Cache-Control': 'no-store',
    });
    resp.end(GIF);
  },
);

フレームワークの最新ドキュメントと照らし合わせて確認済みです。

確認

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

公式サイト: Encore.ts

TypeScript の他の例