proxy-server.js 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. const http = require('http');
  2. const httpProxy = require('http-proxy');
  3. const fs = require('fs');
  4. const path = require('path');
  5. const url = require('url');
  6. const proxy = httpProxy.createProxyServer({});
  7. const PORT = 8090;
  8. const STATIC_DIR = 'C:\\inetpub\\wwwroot\\DRG';
  9. const server = http.createServer((req, res) => {
  10. // 设置 CORS 头
  11. res.setHeader('Access-Control-Allow-Origin', '*');
  12. res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS');
  13. res.setHeader('Access-Control-Allow-Headers', 'Content-Type');
  14. if (req.method === 'OPTIONS') {
  15. res.writeHead(204);
  16. res.end();
  17. return;
  18. }
  19. console.log('Request:', req.url);
  20. // API proxy - 处理 /iris-api/invoke
  21. if (req.url.startsWith('/iris-api/')) {
  22. const targetUrl = 'http://111.229.137.113:52773/csp/drg/sysInternalMutiple' + req.url.substring('/iris-api'.length);
  23. console.log('Proxying to:', targetUrl);
  24. proxy.web(req, res, {
  25. target: 'http://111.229.137.113:52773',
  26. changeOrigin: true,
  27. pathRewrite: {
  28. '^/iris-api': '/csp/drg/sysInternalMutiple'
  29. }
  30. }, (err) => {
  31. console.error('Proxy error:', err);
  32. res.writeHead(500);
  33. res.end(JSON.stringify({ errorCode: '-1', errorMessage: '代理错误: ' + err.message }));
  34. });
  35. return;
  36. }
  37. // Static files
  38. let filePath = path.join(STATIC_DIR, req.url === '/' ? 'index.html' : req.url);
  39. if (!fs.existsSync(filePath) || fs.statSync(filePath).isDirectory()) {
  40. filePath = path.join(STATIC_DIR, 'index.html');
  41. }
  42. const ext = path.extname(filePath);
  43. const contentType = {
  44. '.html': 'text/html',
  45. '.js': 'application/javascript',
  46. '.css': 'text/css',
  47. '.json': 'application/json',
  48. '.png': 'image/png',
  49. '.jpg': 'image/jpeg',
  50. '.svg': 'image/svg+xml'
  51. }[ext] || 'application/octet-stream';
  52. fs.readFile(filePath, (err, data) => {
  53. if (err) {
  54. console.error('File error:', err);
  55. res.writeHead(404);
  56. res.end('Not found');
  57. return;
  58. }
  59. res.setHeader('Content-Type', contentType);
  60. res.writeHead(200);
  61. res.end(data);
  62. });
  63. });
  64. proxy.on('error', (err, req, res) => {
  65. console.error('Proxy error:', err);
  66. res.writeHead(500);
  67. res.end(JSON.stringify({ errorCode: '-1', errorMessage: '代理错误' }));
  68. });
  69. server.listen(PORT, '0.0.0.0', () => {
  70. console.log('Server running on http://0.0.0.0:' + PORT);
  71. console.log('Static dir:', STATIC_DIR);
  72. });