| 1234567891011121314151617181920212223242526272829 |
- // 本地预览:node server.mjs → http://localhost:4173
- // 仅用于演示与截图,不参与任何构建。
- import { createServer } from 'node:http';
- import { readFile, stat } from 'node:fs/promises';
- import { extname, join, normalize } from 'node:path';
- const ROOT = new URL('.', import.meta.url).pathname.replace(/^\/([A-Za-z]:)/, '$1');
- const PORT = Number(process.env.PORT || 4173);
- const TYPES = {
- '.html': 'text/html; charset=utf-8', '.css': 'text/css; charset=utf-8',
- '.js': 'text/javascript; charset=utf-8', '.svg': 'image/svg+xml',
- '.woff2': 'font/woff2', '.json': 'application/json',
- };
- createServer(async (req, res) => {
- const url = decodeURIComponent((req.url || '/').split('?')[0]);
- let path = normalize(join(ROOT, url === '/' ? 'index.html' : url));
- if (!path.startsWith(normalize(ROOT))) { res.writeHead(403).end(); return; }
- try {
- const info = await stat(path);
- if (info.isDirectory()) path = join(path, 'index.html');
- const body = await readFile(path);
- res.writeHead(200, { 'content-type': TYPES[extname(path)] || 'application/octet-stream', 'cache-control': 'no-store' });
- res.end(body);
- } catch {
- const body = await readFile(join(ROOT, '404.html')).catch(() => 'Not found');
- res.writeHead(404, { 'content-type': 'text/html; charset=utf-8' }).end(body);
- }
- }).listen(PORT, () => console.log(`Fmode 落地页预览: http://localhost:${PORT}`));
|