emptygif
Wika: Filipino

Lahat ng language at framework

Node.js standard library (node:http) JavaScript

Isang JavaScript snippet para sa Node.js standard library (node:http) na sumasagot sa mga request sa /pixel.gif gamit ang empty GIF, na isang beses lang dine-decode sa startup.

Node.js standard library (node:http) JavaScript

File
server.mjs
Patakbuhin
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);

Pinatakbo namin ang snippet na ito at ikinumpara ang mga byte na ibinalik nito.

Suriin

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

Opisyal na site: Node.js standard library (node:http)

Iba pa sa JavaScript