/
schoolproject
/
offlinewebschollproject
Обзор
Документация
Войти
/
schoolproject
/
offlinewebschollproject
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
server.js
74 строки
2 KB
schoolproject
create: app.js, index.html, package.json, server.js, start_server.bat, start_server.py, start_server.sh, styles.css
14 мар 2026, 20:29
Верифицирован
14 мар 2026, 20:29
ea4e370
Код
Авторство
О чём код?
const http = require('http'); const fs = require('fs'); const path = require('path'); const { exec } = require('child_process'); const PORT = 8000; const MIME_TYPES = { '.html': 'text/html', '.css': 'text/css', '.js': 'application/javascript', '.json': 'application/json', '.png': 'image/png', '.jpg': 'image/jpeg', '.gif': 'image/gif', '.svg': 'image/svg+xml', '.ico': 'image/x-icon' }; const server = http.createServer((req, res) => { let filePath = '.' + req.url; if (filePath === './') { filePath = './index.html'; } const extname = String(path.extname(filePath)).toLowerCase(); const contentType = MIME_TYPES[extname] || 'application/octet-stream'; fs.readFile(filePath, (error, content) => { if (error) { if (error.code === 'ENOENT') { res.writeHead(404, { 'Content-Type': 'text/html' }); res.end('<h1>404 - Файл не найден</h1>', 'utf-8'); } else { res.writeHead(500); res.end(`Ошибка сервера: ${error.code}`, 'utf-8'); } } else { res.writeHead(200, { 'Content-Type': contentType, 'Cache-Control': 'no-cache, no-store, must-revalidate', 'Pragma': 'no-cache', 'Expires': '0' }); res.end(content, 'utf-8'); } }); }); server.listen(PORT, () => { const url = `http://localhost:${PORT}`; console.log('='.repeat(60)); console.log('Сервер запущен!'); console.log(`Откройте в браузере: ${url}`); console.log('='.repeat(60)); console.log('Для остановки нажмите Ctrl+C'); console.log('='.repeat(60)); const platform = process.platform; let command; if (platform === 'win32') { command = `start ${url}`; } else if (platform === 'darwin') { command = `open ${url}`; } else { command = `xdg-open ${url}`; } exec(command, (error) => { if (error) { console.log('Не удалось автоматически открыть браузер. Откройте вручную.'); } }); });