emptygif
Idioma: Español

Todos los lenguajes y frameworks

NestJS TypeScript

Un fragmento en TypeScript para NestJS que responde a las peticiones a /pixel.gif con el GIF vacío, decodificado una sola vez al arrancar.

NestJS TypeScript

Instalar
npm i @nestjs/core @nestjs/common @nestjs/platform-express reflect-metadata rxjs
Archivo
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();

Ejecutamos este fragmento y comparamos los bytes que devolvió.

Comprobar

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

Sitio oficial: NestJS

Más en TypeScript