/
dyskolas
/
portfolioAI
Обзор
Документация
Войти
/
dyskolas
/
portfolioAI
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
server.mjs
68 строк
2 KB
dyskolas
МЯУ
10 авг 2026, 03:08
Верифицирован
10 авг 2026, 03:08
8e7cd5b
Код
Авторство
О чём код?
import { createServer } from "node:http"; import { createReadStream } from "node:fs"; import { stat } from "node:fs/promises"; import { extname, join, normalize } from "node:path"; const port = Number(process.env.PORT || 4173); const root = process.cwd(); const mime = { ".css": "text/css; charset=utf-8", ".html": "text/html; charset=utf-8", ".js": "text/javascript; charset=utf-8", ".json": "application/json; charset=utf-8", ".mp4": "video/mp4", ".png": "image/png", ".svg": "image/svg+xml", ".webp": "image/webp" }; createServer(async (request, response) => { try { const pathname = decodeURIComponent(new URL(request.url || "/", "http://localhost").pathname); const relative = pathname === "/" ? "index.html" : pathname.replace(/^\/+/, ""); let target = normalize(join(root, relative)); if (!target.startsWith(root)) { response.writeHead(403).end("Forbidden"); return; } let info = await stat(target); if (info.isDirectory()) { target = join(target, "index.html"); info = await stat(target); } const range = request.headers.range; const headers = { "Accept-Ranges": "bytes", "Cache-Control": "no-cache", "Content-Type": mime[extname(target).toLowerCase()] || "application/octet-stream" }; if (range && /^bytes=\d*-\d*$/.test(range)) { const [rawStart, rawEnd] = range.replace("bytes=", "").split("-"); const start = rawStart ? Number(rawStart) : 0; const end = rawEnd ? Math.min(Number(rawEnd), info.size - 1) : info.size - 1; if (start > end || start >= info.size) { response.writeHead(416, { "Content-Range": `bytes */${info.size}` }).end(); return; } response.writeHead(206, { ...headers, "Content-Length": end - start + 1, "Content-Range": `bytes ${start}-${end}/${info.size}` }); if (request.method === "HEAD") response.end(); else createReadStream(target, { start, end }).pipe(response); return; } response.writeHead(200, { ...headers, "Content-Length": info.size }); if (request.method === "HEAD") response.end(); else createReadStream(target).pipe(response); } catch { response.writeHead(404, { "Content-Type": "text/plain; charset=utf-8" }).end("Not found"); } }).listen(port, "127.0.0.1", () => { console.log(`Aleksei Artemov Portfolio: http://localhost:${port}`); });