server.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /* eslint no-console: 0 */
  2. const express = require('express');
  3. const http = require('http');
  4. const httpProxy = require('http-proxy');
  5. const HttpProxyRules = require('http-proxy-rules');
  6. const app = express();
  7. // const BACKEND_HOST = process.env.BACKEND_HOST || 'localhost';
  8. const BACKEND_HOST = '39.106.74.84'
  9. /*
  10. *
  11. * Proxy requests to:
  12. * - /api -> :4040/api
  13. *
  14. */
  15. // http://observe-server.cestong.com.cn/api/v1/service/graph?start_time=0&end_time=0&app_alias=UNSET
  16. const backendProxy = httpProxy.createProxy({
  17. target: `http://${BACKEND_HOST}:4040`,
  18. // target:'http://observe-server.cestong.com.cn',
  19. ws: true,
  20. });
  21. backendProxy.on('error', err => console.error('Proxy error', err));
  22. app.all('/api*', backendProxy.web.bind(backendProxy));
  23. /*
  24. *
  25. * Production env serves precompiled content from build/
  26. *
  27. */
  28. if (process.env.NODE_ENV === 'production') {
  29. app.use(express.static('build'));
  30. }
  31. /*
  32. *
  33. * Webpack Dev Middleware with Hot Reload
  34. *
  35. * See: https://github.com/webpack/webpack-dev-middleware;
  36. * https://github.com/glenjamin/webpack-hot-middleware
  37. *
  38. */
  39. if (process.env.NODE_ENV !== 'production') {
  40. const webpack = require('webpack');
  41. const webpackMiddleware = require('webpack-dev-middleware');
  42. const webpackHotMiddleware = require('webpack-hot-middleware');
  43. const config = require('./webpack.local.config');
  44. const compiler = webpack(config);
  45. app.use(webpackMiddleware(compiler, {
  46. noInfo: true,
  47. publicPath: config.output.publicPath, // required
  48. stats: 'errors-only',
  49. }));
  50. app.use(webpackHotMiddleware(compiler));
  51. }
  52. /*
  53. *
  54. * Express server
  55. *
  56. */
  57. const port = process.env.PORT || 4042;
  58. const server = app.listen(port, 'localhost', () => {
  59. const host = server.address().address;
  60. });
  61. server.on('upgrade', backendProxy.ws.bind(backendProxy));
  62. /*
  63. *
  64. * Path proxy server
  65. *
  66. */
  67. const proxyRules = new HttpProxyRules({
  68. rules: {
  69. '/scoped/': `http://localhost:${port}`,
  70. }
  71. });
  72. const pathProxy = httpProxy.createProxy({ws: true});
  73. pathProxy.on('error', err => console.error('path proxy error', err));
  74. const pathProxyPort = port + 1;
  75. const proxyPathServer = http.createServer((req, res) => {
  76. const target = proxyRules.match(req);
  77. if (!target) {
  78. res.writeHead(500, {'Content-Type': 'text/plain'});
  79. return res.end('No rules matched! Check out /scoped/');
  80. }
  81. return pathProxy.web(req, res, {target});
  82. }).listen(pathProxyPort, 'localhost', () => {
  83. const pathProxyHost = proxyPathServer.address().address;
  84. console.log(
  85. 'Scope Proxy Path UI listening at http://%s:%s/scoped/',
  86. pathProxyHost, pathProxyPort
  87. );
  88. });
  89. proxyPathServer.on('upgrade', (req, socket, head) => {
  90. const target = proxyRules.match(req);
  91. if (target) {
  92. pathProxy.ws(req, socket, head, {target});
  93. }
  94. });