SPB Git forge

spb/job-ka

Public
229commits 1branches 0releases
38.1 MBsize
maindefault branch
1 h agolast push
HTML 82.1% Python 14.6% TypeScript 1.9% CSS 1% JavaScript 0.5%
1.7 KB · 29 lines javascript
Raw Blame History
1// Serveur de prévisualisation QA (zéro dépendance) : sert un build Vite2// (dist-next par défaut) et relaie /api vers le backend local — permet de3// valider une fiche avec Playwright SANS toucher au dist/ servi en production.4// Usage : node frontend/scripts/preview.mjs [dir=dist-next] [port=18096] [api=http://127.0.0.1:8096]5import http from "node:http";6import { createReadStream, existsSync, statSync } from "node:fs";7import { extname, join, resolve } from "node:path";89const DIR = resolve(process.argv[2] || "dist-next");10const PORT = Number(process.argv[3] || 18096);11const API = new URL(process.argv[4] || "http://127.0.0.1:8096");12const MIME = { ".html": "text/html; charset=utf-8", ".js": "text/javascript", ".css": "text/css", ".svg": "image/svg+xml",13  ".png": "image/png", ".json": "application/json", ".geojson": "application/geo+json", ".woff2": "font/woff2", ".ico": "image/x-icon" };1415http.createServer((req, res) => {16  const url = new URL(req.url, "http://x");17  if (url.pathname.startsWith("/api/")) {18    const p = http.request({ host: API.hostname, port: API.port, path: req.url, method: req.method, headers: { ...req.headers, host: API.host } },19      (r) => { res.writeHead(r.statusCode, r.headers); r.pipe(res); });20    p.on("error", () => { res.writeHead(502); res.end("api down"); });21    req.pipe(p);22    return;23  }24  let f = join(DIR, decodeURIComponent(url.pathname));25  if (!existsSync(f) || statSync(f).isDirectory()) f = join(DIR, "index.html");26  res.writeHead(200, { "content-type": MIME[extname(f)] || "application/octet-stream" });27  createReadStream(f).pipe(res);28}).listen(PORT, "127.0.0.1", () => console.log(`preview ${DIR} → http://127.0.0.1:${PORT} (api → ${API.origin})`));29