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 の他の例