emptygif
语言: 简体中文

所有语言和框架

Encore.ts TypeScript

一个用于 Encore.ts 的 TypeScript 代码片段,以空 GIF 响应对 /pixel.gif 的请求,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 示例