Files
bus-capacitor-calculator/scripts/serve.cjs
T
gaochengyu 73ab36f7ff feat: 开源母线电容计算器并接入独立自动部署
整理 Buck 与 PMSM 完整源码、离线构建、校核测试和使用说明,采用 MIT 许可。
增加页首开源链接,支持推送 main 后自动测试、构建并发布到原计算器网址。
2026-11-27 04:38:28 +08:00

22 lines
1.2 KiB
JavaScript

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));