76 lines
2.1 KiB
JavaScript
76 lines
2.1 KiB
JavaScript
/* Minimal static dev server for the component showcase site.
|
|
Usage (from repo root): node site/dev-server.js
|
|
Then open: http://127.0.0.1:3311/site/
|
|
Serves the repo root so /site, /frameworks and /.design_library are all reachable. */
|
|
'use strict';
|
|
const http = require('http');
|
|
const fs = require('fs');
|
|
const path = require('path');
|
|
|
|
const PORT = 3311;
|
|
const HOST = '127.0.0.1';
|
|
const ROOT = process.cwd();
|
|
|
|
const TYPES = {
|
|
'.html': 'text/html; charset=utf-8',
|
|
'.css': 'text/css; charset=utf-8',
|
|
'.js': 'text/javascript; charset=utf-8',
|
|
'.mjs': 'text/javascript; charset=utf-8',
|
|
'.json': 'application/json; charset=utf-8',
|
|
'.txt': 'text/plain; charset=utf-8',
|
|
'.vue': 'text/plain; charset=utf-8',
|
|
'.svg': 'image/svg+xml',
|
|
'.png': 'image/png',
|
|
'.ico': 'image/x-icon'
|
|
};
|
|
|
|
const server = http.createServer((req, res) => {
|
|
let urlPath;
|
|
try {
|
|
urlPath = decodeURIComponent(req.url.split('?')[0]);
|
|
} catch (e) {
|
|
res.writeHead(400);
|
|
res.end('Bad request');
|
|
return;
|
|
}
|
|
if (urlPath === '/' || urlPath === '') {
|
|
res.writeHead(302, { Location: '/site/' });
|
|
res.end();
|
|
return;
|
|
}
|
|
if (urlPath.endsWith('/')) urlPath += 'index.html';
|
|
// 根路径与常见入口自动跳转到站点,避免裸访问 404
|
|
if (urlPath === '/index.html') {
|
|
res.writeHead(302, { Location: '/site/' });
|
|
res.end();
|
|
return;
|
|
}
|
|
const filePath = path.join(ROOT, urlPath);
|
|
// 防目录穿越
|
|
if (!filePath.startsWith(ROOT)) {
|
|
res.writeHead(403);
|
|
res.end('Forbidden');
|
|
return;
|
|
}
|
|
fs.readFile(filePath, (err, buf) => {
|
|
if (err) {
|
|
res.writeHead(404, { 'Content-Type': 'text/plain; charset=utf-8' });
|
|
res.end('404 Not Found: ' + urlPath);
|
|
return;
|
|
}
|
|
res.writeHead(200, {
|
|
'Content-Type': TYPES[path.extname(filePath).toLowerCase()] || 'application/octet-stream',
|
|
'Cache-Control': 'no-cache'
|
|
});
|
|
res.end(buf);
|
|
});
|
|
});
|
|
|
|
server.listen(PORT, HOST, () => {
|
|
console.log('Aurora Admin showcase serving at http://' + HOST + ':' + PORT + '/site/');
|
|
});
|
|
server.on('error', (e) => {
|
|
console.error('Server error:', e.message);
|
|
process.exit(1);
|
|
});
|