hotServer.js 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. const express = require('express');
  2. const debug = require('debug')('app:server');
  3. const webpack = require('webpack');
  4. const proxy = require('http-proxy-middleware');
  5. const webpackConfig = require('../build/webpack.config');
  6. const config = require('../config');
  7. const app = express();
  8. const paths = config.utils_paths;
  9. for (let i in config.proxy) {
  10. app.use(
  11. proxy(config.proxy[i].from, {
  12. target: config.proxy[i].target,
  13. changeOrigin: true,
  14. pathRewrite: {
  15. [config.proxy[i].from]: config.proxy[i].to,
  16. },
  17. }),
  18. );
  19. }
  20. app.use(require('connect-history-api-fallback')());
  21. const compiler = webpack(webpackConfig);
  22. debug('Enable webpack dev and HMR middleware');
  23. app.use(
  24. require('webpack-dev-middleware')(compiler, {
  25. publicPath: webpackConfig.output.publicPath,
  26. contentBase: paths.client(),
  27. hot: true,
  28. lazy: false,
  29. inline: true,
  30. }),
  31. );
  32. debug('start hot server!');
  33. app.use(require('webpack-hot-middleware')(compiler));
  34. app.use('/assets', express.static(paths.client('assets')));
  35. app.use('/assets', express.static(paths.project('assets')));
  36. app.use(express.static(paths.client('static')));
  37. app.use(express.static(paths.project('static')));
  38. app.use(express.static(paths.lib()));
  39. module.exports = app;