emptygif
Idioma: Español

Todos los lenguajes y frameworks

Node.js standard library (node:http) JavaScript

Un fragmento en JavaScript para Node.js standard library (node:http) que responde a las peticiones a /pixel.gif con el GIF vacío, decodificado una sola vez al arrancar.

Node.js standard library (node:http) JavaScript

Archivo
server.mjs
Ejecutar
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);

Ejecutamos este fragmento y comparamos los bytes que devolvió.

Comprobar

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

Sitio oficial: Node.js standard library (node:http)

Más en JavaScript