import http from 'node:http'; import fs from 'node:fs'; import path from 'node:path'; const root=path.resolve(process.argv.includes('--public')?'public':'dist'); const port=Number(process.env.PORT||4173); const types={'.html':'text/html; charset=utf-8','.js':'text/javascript; charset=utf-8','.mjs':'text/javascript; charset=utf-8','.css':'text/css; charset=utf-8','.json':'application/json; charset=utf-8','.svg':'image/svg+xml','.png':'image/png','.jpg':'image/jpeg','.jpeg':'image/jpeg','.webp':'image/webp','.gif':'image/gif','.ico':'image/x-icon','.woff2':'font/woff2','.woff':'font/woff','.ttf':'font/ttf','.wasm':'application/wasm','.txt':'text/plain; charset=utf-8'}; http.createServer((req,res)=>{ let pathname; try{pathname=decodeURIComponent(new URL(req.url,'http://localhost').pathname);}catch{res.writeHead(400);res.end();return;} let file=path.resolve(root,'.'+pathname); if(file!==root&&!file.startsWith(root+path.sep)){res.writeHead(403);res.end();return;} if(fs.existsSync(file)&&fs.statSync(file).isDirectory())file=path.join(file,'index.html'); if(!fs.existsSync(file)&&!path.extname(file))file=path.join(file,'index.html'); if(!fs.existsSync(file)){res.writeHead(404,{'content-type':'text/html; charset=utf-8'});res.end('快照中未包含此页面

此页面未包含在本地快照中

当前站点为纯静态复刻版。

返回首页');return;} res.writeHead(200,{'content-type':types[path.extname(file)]||'application/octet-stream','cache-control':'no-cache'}); fs.createReadStream(file).pipe(res); }).listen(port,'127.0.0.1',()=>console.log(`Static preview: http://127.0.0.1:${port} (root ${root})`));