智慧水务管理系统 - 精河县供水工程综合管理平台

frontend-serve.js 847B

1234567891011121314151617181920212223242526272829303132
  1. const http = require('http');
  2. const fs = require('fs');
  3. const path = require('path');
  4. const PORT = process.env.FRONTEND_PORT || 8080;
  5. const server = http.createServer((req, res) => {
  6. let filePath = path.join(__dirname, req.url === '/' ? 'index.html' : req.url);
  7. const ext = path.extname(filePath);
  8. const contentTypes = {
  9. '.html': 'text/html',
  10. '.css': 'text/css',
  11. '.js': 'application/javascript',
  12. '.png': 'image/png',
  13. '.jpg': 'image/jpeg',
  14. '.svg': 'image/svg+xml'
  15. };
  16. fs.readFile(filePath, (err, data) => {
  17. if (err) {
  18. res.writeHead(404);
  19. res.end('Not Found');
  20. } else {
  21. res.writeHead(200, { 'Content-Type': contentTypes[ext] || 'text/plain' });
  22. res.end(data);
  23. }
  24. });
  25. });
  26. server.listen(PORT, () => {
  27. console.log(`🚀 前端服务已启动:http://localhost:${PORT}`);
  28. });