emptygif
Sprache: Deutsch

Alle Sprachen und Frameworks

Node.js standard library (node:http) JavaScript

Ein JavaScript-Snippet für Node.js standard library (node:http), das Anfragen an /pixel.gif mit dem leeren GIF beantwortet, das einmal beim Start dekodiert wird.

Node.js standard library (node:http) JavaScript

Datei
server.mjs
Ausführen
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);

Wir haben dieses Snippet ausgeführt und die zurückgegebenen Bytes verglichen.

Prüfen

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

Offizielle Website: Node.js standard library (node:http)

Mehr in JavaScript