const http = require('node:http'); const fs = require('node:fs'); const path = require('node:path'); const root = path.resolve(__dirname, '../dist'); const port = Number(process.argv[2] || 8080); if (!fs.existsSync(path.join(root, 'impedance-calculators.html'))) { console.error('Run npm run build first.'); process.exit(1); } http.createServer((request, response) => { try { let pathname = decodeURIComponent(new URL(request.url, 'http://localhost').pathname); if (pathname === '/') pathname = '/impedance-calculators.html'; const file = path.resolve(root, '.' + pathname); if (!file.startsWith(root + path.sep) || !fs.statSync(file).isFile()) throw Error('Not found'); const type = file.endsWith('.html') ? 'text/html; charset=utf-8' : file.endsWith('.json') ? 'application/json; charset=utf-8' : 'text/plain; charset=utf-8'; response.writeHead(200, { 'Content-Type': type, 'Cache-Control': 'no-cache' }); fs.createReadStream(file).pipe(response); } catch { response.writeHead(404, { 'Content-Type': 'text/plain; charset=utf-8' }); response.end('Not found'); } }).listen(port, '127.0.0.1', () => console.log('Calculator: http://127.0.0.1:' + port));