const http = require('http'); const fs = require('fs'); const path = require('path'); const root = __dirname; const types = { '.html': 'text/html; charset=utf-8', '.png': 'image/png', '.css': 'text/css; charset=utf-8', '.js': 'text/javascript; charset=utf-8', '.md': 'text/plain; charset=utf-8' }; const server = http.createServer((req, res) => { try { const url = new URL(req.url, 'http://127.0.0.1:18765'); const rel = decodeURIComponent(url.pathname).replace(/^\/+/, ''); const filePath = path.resolve(root, rel); if (!filePath.startsWith(path.resolve(root))) { res.writeHead(403); res.end('Forbidden'); return; } fs.readFile(filePath, (err, data) => { if (err) { res.writeHead(404); res.end('Not found: ' + filePath); return; } res.writeHead(200, { 'Content-Type': types[path.extname(filePath).toLowerCase()] || 'application/octet-stream' }); res.end(data); }); } catch (e) { res.writeHead(500); res.end(String(e)); } }); server.listen(18765, '127.0.0.1', () => console.log('preview server on 18765')); setInterval(() => {}, 1000);