Source identity
| Field | Recorded value |
|---|---|
| Source type | GitHub source |
| Original record | Janady13/thatdeveloperguy-website / scripts/serve-dist.ts |
| Date | Not specified in source |
| Repository | Janady13/thatdeveloperguy-website |
| Revision | 2bea2ebdff0e34494bff597d4fa9c8422a0c3a94 |
| Branch | main |
| Path or archive member | scripts/serve-dist.ts |
| Original SHA-256 | 9f69cfd916e017260c212e53e260b04ecc9e73237a9ec99aa73558cfde66d120 |
| Git blob SHA | 99b9b7bd74429329b6574dbaa1981cdbe34e58b2 |
| Reading-copy SHA-256 | 9f69cfd916e017260c212e53e260b04ecc9e73237a9ec99aa73558cfde66d120 |
Recovered source transcript
Download complete source text ↓19 lines · 1.5 KB
/** Serves dist/site with the same rules as the nginx vhost (no SPA fallback, real 404s, trailing-slash redirect) for local browser tests. */
import { createReadStream, existsSync, statSync } from 'node:fs';
import { createServer } from 'node:http';
import { extname, join, resolve } from 'node:path';
const site = resolve(import.meta.dirname, '../dist/site');
const port = Number(process.env.PORT ?? 4411);
const TYPES: Record<string, string> = { '.html': 'text/html; charset=utf-8', '.js': 'text/javascript', '.css': 'text/css', '.svg': 'image/svg+xml', '.json': 'application/json', '.wasm': 'application/wasm', '.riv': 'application/octet-stream', '.woff2': 'font/woff2', '.txt': 'text/plain', '.xml': 'application/xml', '.png': 'image/png', '.jpg': 'image/jpeg' };
createServer((req, res) => {
const url = new URL(req.url ?? '/', 'http://x');
let path = decodeURIComponent(url.pathname);
if (path.length > 1 && path.endsWith('/')) { res.writeHead(301, { location: path.replace(/\/+$/, '') + url.search }); res.end(); return; }
const send = (file: string, status = 200) => { res.writeHead(status, { 'content-type': TYPES[extname(file)] ?? 'application/octet-stream', 'x-robots-tag': 'noindex' }); createReadStream(file).pipe(res); };
const direct = join(site, path);
if (existsSync(direct) && statSync(direct).isFile()) return send(direct);
const index = join(site, path, 'index.html');
if (existsSync(index)) return send(index);
send(join(site, '404.html'), 404);
}).listen(port, '127.0.0.1', () => console.log(`dist/site on http://127.0.0.1:${port}`));