emptygif
언어: 한국어

모든 언어 및 프레임워크

NestJS TypeScript

/pixel.gif 요청에 빈 GIF로 응답하는 NestJS용 TypeScript 스니펫입니다. GIF는 시작할 때 한 번만 디코딩합니다.

NestJS TypeScript

설치
npm i @nestjs/core @nestjs/common @nestjs/platform-express reflect-metadata rxjs
파일
main.ts
import { Controller, Get, Header, Module, StreamableFile } from '@nestjs/common';
import { NestFactory } from '@nestjs/core';

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

@Controller()
class PixelController {
  @Get('/pixel.gif')
  @Header('Cache-Control', 'no-store')
  pixel(): StreamableFile {
    // A plain Buffer would be serialised as JSON; StreamableFile sends raw bytes.
    return new StreamableFile(GIF, { type: 'image/gif', length: GIF.length });
  }
}

@Module({ controllers: [PixelController] })
class AppModule {}

async function bootstrap() {
  const app = await NestFactory.create(AppModule);
  await app.listen(8080);
}
bootstrap();

이 스니펫은 실행해 보고, 반환된 바이트를 비교했습니다.

확인

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

공식 사이트: NestJS

TypeScript의 다른 예제