| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748 |
- const http = require('http');
- const fs = require('fs');
- const path = require('path');
- const PORT = 8081;
-
- const server = http.createServer((req, res) => {
- let filePath = path.join(__dirname, req.url === '/' ? 'index.html' : req.url);
- const ext = path.extname(filePath);
- const contentTypes = {
- '.html': 'text/html',
- '.css': 'text/css',
- '.js': 'application/javascript',
- '.png': 'image/png',
- '.jpg': 'image/jpeg',
- '.gif': 'image/gif',
- '.svg': 'image/svg+xml',
- '.ico': 'image/x-icon'
- };
-
- console.log(`[${new Date().toISOString()}] 请求:${req.url} -> ${filePath}`);
-
- fs.readFile(filePath, (err, data) => {
- if (err) {
- console.error(`文件读取失败:${filePath}`);
- res.writeHead(404, { 'Content-Type': 'text/html' });
- res.end('<h1>404 - 文件未找到</h1>');
- } else {
- const contentType = contentTypes[ext] || 'application/octet-stream';
- console.log(`文件读取成功:${filePath} (${contentType})`);
- res.writeHead(200, { 'Content-Type': contentType });
- res.end(data);
- }
- });
- });
-
- server.listen(PORT, () => {
- console.log(`================================`);
- console.log(`🚀 中小企业 CRM 前端服务已启动`);
- console.log(`📡 端口:${PORT}`);
- console.log(`🌐 访问地址:http://localhost:${PORT}`);
- console.log(`📁 文件目录:${__dirname}`);
- console.log(`================================`);
- });
-
- server.on('error', (err) => {
- console.error(`服务器错误:${err.message}`);
- process.exit(1);
- });
|