emptygif
மொழி: தமிழ்

அனைத்து மொழிகளும் ஃப்ரேம்வொர்க்குகளும்

NestJS TypeScript

/pixel.gif க்கு வரும் ரெக்வெஸ்ட்களுக்கு empty 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 இல் மேலும்