Node.js standard library (node:http) JavaScript
適用於 Node.js standard library (node:http) 的 JavaScript 程式碼片段,以空 GIF 回應對 /pixel.gif 的請求,GIF 在啟動時只解碼一次。
Node.js standard library (node:http) JavaScript
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)