emptygif
언어: 한국어

모든 언어 및 프레임워크

Node.js standard library (node:http) JavaScript

/pixel.gif 요청에 빈 GIF로 응답하는 Node.js standard library (node:http)용 JavaScript 스니펫입니다. GIF는 시작할 때 한 번만 디코딩합니다.

Node.js standard library (node:http) JavaScript

파일
server.mjs
실행
node server.mjs
import { createServer } from 'node:http';

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

createServer((req, res) => {
  const { pathname } = new URL(req.url, 'http://localhost');
  if (req.method !== 'GET' || pathname !== '/pixel.gif') {
    res.writeHead(404).end();
    return;
  }
  res.writeHead(200, {
    'Content-Type': 'image/gif',
    'Content-Length': GIF.length,
    'Cache-Control': 'no-store',
  });
  res.end(GIF);
}).listen(8080);

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

확인

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

공식 사이트: Node.js standard library (node:http)

JavaScript의 다른 예제