Files
contract-demo/prototype/server.js
D8D Developer 9cd6837935 feat: 生成资产租赁管理系统桌面端原型
- 更新为桌面端侧边栏布局
- 添加数据概览、基础信息、合同管理等页面
- 修改服务器端口为 7358
- 添加原型截图和文档数据

Generated with [Claude Code](https://claude.ai/code)
via [Happy](https://happy.engineering)

Co-Authored-By: Claude <noreply@anthropic.com>
Co-Authored-By: Happy <yesreply@happy.engineering>
2026-03-24 10:30:18 +00:00

89 lines
2.7 KiB
JavaScript

const http = require('http');
const fs = require('fs');
const path = require('path');
const PORT = 7358;
const MIME_TYPES = {
'.html': 'text/html; charset=utf-8',
'.css': 'text/css; charset=utf-8',
'.js': 'application/javascript; charset=utf-8',
'.json': 'application/json; charset=utf-8',
'.png': 'image/png',
'.jpg': 'image/jpeg',
'.svg': 'image/svg+xml',
};
function getMimeType(filepath) {
const ext = path.extname(filepath).toLowerCase();
return MIME_TYPES[ext] || 'application/octet-stream';
}
function readMarkdownFile(filepath) {
try {
const content = fs.readFileSync(filepath, 'utf-8');
return { success: true, content: content, name: path.basename(filepath) };
} catch (error) {
return { success: false, error: error.message };
}
}
function getDocsList() {
const docsDir = path.join(__dirname, '../docs');
try {
const files = fs.readdirSync(docsDir).filter(f => f.endsWith('.md'));
return files.map(file => ({
name: file.replace('.md', ''),
file: file,
path: file.replace('.md', '')
}));
} catch (error) {
return [];
}
}
const server = http.createServer((req, res) => {
// API: 获取文档列表
if (req.url === '/api/docs') {
res.writeHead(200, { 'Content-Type': 'application/json; charset=utf-8' });
res.end(JSON.stringify(getDocsList()));
return;
}
// API: 获取单个文档
if (req.url.startsWith('/api/doc/')) {
const docName = decodeURIComponent(req.url.substring(9));
const docPath = path.join(__dirname, '../docs', docName + '.md');
const result = readMarkdownFile(docPath);
if (result.success) {
res.writeHead(200, { 'Content-Type': 'application/json; charset=utf-8' });
res.end(JSON.stringify(result));
} else {
res.writeHead(404, { 'Content-Type': 'application/json; charset=utf-8' });
res.end(JSON.stringify({ success: false, error: 'Document not found' }));
}
return;
}
// 静态文件服务
let filepath = req.url === '/' ? '/index.html' : req.url;
filepath = path.join(__dirname, filepath);
if (fs.existsSync(filepath) && fs.statSync(filepath).isDirectory()) {
filepath = path.join(filepath, 'index.html');
}
if (fs.existsSync(filepath)) {
const mimeType = getMimeType(filepath);
res.writeHead(200, { 'Content-Type': mimeType });
fs.createReadStream(filepath).pipe(res);
} else {
res.writeHead(404, { 'Content-Type': 'text/html; charset=utf-8' });
res.end('<h1>404</h1>');
}
});
server.listen(PORT, () => {
console.log('Server running at http://localhost:' + PORT);
});