webpack.config.server.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. /*
  2. * @Author: Johnhong9527
  3. * @Date: 2019-06-04 17:08:04
  4. * @Last Modified by: Johnhong9527
  5. * @Last Modified time: 2019-06-05 10:13:27
  6. */
  7. // import app from './app'; // configuring express app, e.g. routes and logic
  8. const app = require('./app.js');
  9. function startServer() {
  10. return new Promise((resolve, reject) => {
  11. const httpServer = app.listen(app.get('port'));
  12. httpServer.once('error', (err) => {
  13. if (err.code === 'EADDRINUSE') {
  14. reject(err);
  15. }
  16. });
  17. httpServer.once('listening', () => resolve(httpServer));
  18. }).then(httpServer => {
  19. const { port } = httpServer.address();
  20. console.info(`==> 🌎 Listening on ${port}. Open up http://localhost:${port}/ in your browser.`);
  21. // Hot Module Replacement API
  22. if (module.hot) {
  23. let currentApp = app;
  24. module.hot.accept('./app', () => {
  25. httpServer.removeListener('request', currentApp);
  26. require('./app')
  27. .then(({ default: nextApp }) => {
  28. currentApp = nextApp;
  29. httpServer.on('request', currentApp);
  30. console.log('HttpServer reloaded!');
  31. })
  32. .catch(err => console.error(err));
  33. });
  34. // For reload main module (self). It will be restart http-server.
  35. module.hot.accept(err => console.error(err));
  36. module.hot.dispose(() => {
  37. console.log('Disposing entry module...');
  38. httpServer.close();
  39. });
  40. }
  41. });
  42. }
  43. console.log('Starting http server...');
  44. startServer().catch(err => {
  45. console.error('Error in server start script.', err);
  46. });