serve.js 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. const http = require('http');
  2. const fs = require('fs');
  3. const path = require('path');
  4. const PORT = 8888;
  5. const DIST_DIR = 'd:\\AI开发\\DRG医保控费预警系统开发项目\\frontend\\dist';
  6. const mimeTypes = {
  7. '.html': 'text/html',
  8. '.js': 'application/javascript',
  9. '.css': 'text/css',
  10. '.json': 'application/json',
  11. '.png': 'image/png',
  12. '.jpg': 'image/jpeg',
  13. '.svg': 'image/svg+xml',
  14. '.ico': 'image/x-icon'
  15. };
  16. const server = http.createServer((req, res) => {
  17. console.log(`${new Date().toISOString()} - ${req.method} ${req.url}`);
  18. let filePath = path.join(DIST_DIR, req.url === '/' ? 'index.html' : req.url);
  19. // Check if file exists
  20. if (!fs.existsSync(filePath)) {
  21. res.writeHead(404);
  22. res.end('File not found');
  23. return;
  24. }
  25. const ext = path.extname(filePath).toLowerCase();
  26. const contentType = mimeTypes[ext] || 'application/octet-stream';
  27. fs.readFile(filePath, (err, content) => {
  28. if (err) {
  29. res.writeHead(500);
  30. res.end('Server error');
  31. return;
  32. }
  33. res.writeHead(200, { 'Content-Type': contentType });
  34. res.end(content);
  35. });
  36. });
  37. server.listen(PORT, () => {
  38. console.log(`Server running at http://localhost:${PORT}/`);
  39. });