start-test-server.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. // 简单的Node.js测试服务器
  2. // 使用方法:node start-test-server.js
  3. const http = require('http');
  4. const fs = require('fs');
  5. const path = require('path');
  6. const { URL } = require('url');
  7. // 后端地址
  8. const BACKEND_HOST = 'localhost';
  9. const BACKEND_PORT = 8080;
  10. // 前端目录
  11. const FRONTEND_DIR = path.join(__dirname, 'RuoYi-Vue3', 'dist');
  12. // 创建HTTP服务器
  13. const server = http.createServer((req, res) => {
  14. const reqUrl = new URL(req.url, `http://${req.headers.host}`);
  15. console.log(`${new Date().toISOString()} ${req.method} ${req.url}`);
  16. // API代理
  17. if (reqUrl.pathname.startsWith('/prod-api/')) {
  18. proxyRequest(req, res, reqUrl);
  19. return;
  20. }
  21. // 静态文件
  22. let filePath = path.join(FRONTEND_DIR, reqUrl.pathname === '/' ? 'index.html' : reqUrl.pathname);
  23. // 如果文件不存在,返回index.html(SPA路由)
  24. if (!fs.existsSync(filePath)) {
  25. filePath = path.join(FRONTEND_DIR, 'index.html');
  26. }
  27. // 读取文件
  28. fs.readFile(filePath, (err, data) => {
  29. if (err) {
  30. res.writeHead(404);
  31. res.end('File not found');
  32. return;
  33. }
  34. // 设置Content-Type
  35. const ext = path.extname(filePath);
  36. const contentTypes = {
  37. '.html': 'text/html',
  38. '.js': 'application/javascript',
  39. '.css': 'text/css',
  40. '.json': 'application/json',
  41. '.png': 'image/png',
  42. '.jpg': 'image/jpeg',
  43. '.gif': 'image/gif',
  44. '.svg': 'image/svg+xml',
  45. '.ico': 'image/x-icon'
  46. };
  47. res.setHeader('Content-Type', contentTypes[ext] || 'application/octet-stream');
  48. res.writeHead(200);
  49. res.end(data);
  50. });
  51. });
  52. // 代理请求到后端
  53. function proxyRequest(req, res, reqUrl) {
  54. const options = {
  55. hostname: BACKEND_HOST,
  56. port: BACKEND_PORT,
  57. path: req.url,
  58. method: req.method,
  59. headers: req.headers
  60. };
  61. const proxyReq = http.request(options, (proxyRes) => {
  62. res.writeHead(proxyRes.statusCode, proxyRes.headers);
  63. proxyRes.pipe(res);
  64. });
  65. proxyReq.on('error', (err) => {
  66. console.error('Proxy error:', err);
  67. res.writeHead(502);
  68. res.end('Bad Gateway');
  69. });
  70. req.pipe(proxyReq);
  71. }
  72. // 启动服务器
  73. const PORT = 8088;
  74. server.listen(PORT, () => {
  75. console.log('========================================');
  76. console.log('测试服务器已启动');
  77. console.log('========================================');
  78. console.log(`前端地址: http://localhost:${PORT}`);
  79. console.log(`后端地址: http://${BACKEND_HOST}:${BACKEND_PORT}/prod-api`);
  80. console.log('========================================');
  81. console.log('请在浏览器中打开: http://localhost:8088');
  82. console.log('========================================');
  83. console.log('按 Ctrl+C 停止服务器');
  84. });