Files
Harvey_Team_Page/scripts/dev-server.mjs
T
2026-10-13 11:01:30 +03:00

43 lines
1.9 KiB
JavaScript

// dev-server.mjs — локальная раздача проекта БЕЗ HTTP-кэша.
// Зачем: python -m http.server не шлёт Cache-Control, и браузер держит старые js/css
// во вкладке — правки «не видны» до жёсткого reload (Ctrl+Shift+R). Здесь no-store,
// поэтому обычного F5 достаточно.
//
// Запуск: node scripts/dev-server.mjs [порт] (по умолчанию 8765)
// Страница: http://127.0.0.1:8765/src/
import { createServer } from 'node:http';
import { createReadStream } from 'node:fs';
import { stat } from 'node:fs/promises';
import { extname, join, normalize } from 'node:path';
import { fileURLToPath } from 'node:url';
const ROOT = fileURLToPath(new URL('..', import.meta.url));
const PORT = Number(process.argv[2]) || 8765;
const TYPES = {
'.html': 'text/html; charset=utf-8', '.js': 'text/javascript; charset=utf-8',
'.css': 'text/css; charset=utf-8', '.json': 'application/json; charset=utf-8',
'.svg': 'image/svg+xml', '.png': 'image/png', '.jpg': 'image/jpeg', '.jpeg': 'image/jpeg',
'.webp': 'image/webp', '.ico': 'image/x-icon',
};
createServer(async (req, res) => {
const url = decodeURIComponent((req.url || '/').split('?')[0]);
let file = normalize(join(ROOT, url));
if (!file.startsWith(ROOT)) { res.writeHead(403); res.end(); return; }
try {
if ((await stat(file)).isDirectory()) file = join(file, 'index.html');
const info = await stat(file);
res.writeHead(200, {
'Content-Type': TYPES[extname(file).toLowerCase()] || 'application/octet-stream',
'Content-Length': info.size,
'Cache-Control': 'no-store, must-revalidate',
});
createReadStream(file).pipe(res);
} catch {
res.writeHead(404, { 'Content-Type': 'text/plain; charset=utf-8' });
res.end('404');
}
}).listen(PORT, '127.0.0.1', () => {
console.log(`dev (no-cache): http://127.0.0.1:${PORT}/src/`);
});