123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051 |
- /*
- * @Author: Johnhong9527
- * @Date: 2019-06-04 17:08:04
- * @Last Modified by: Johnhong9527
- * @Last Modified time: 2019-06-05 10:13:27
- */
- // import app from './app'; // configuring express app, e.g. routes and logic
- const app = require('./app.js');
- function startServer() {
- return new Promise((resolve, reject) => {
- const httpServer = app.listen(app.get('port'));
- httpServer.once('error', (err) => {
- if (err.code === 'EADDRINUSE') {
- reject(err);
- }
- });
- httpServer.once('listening', () => resolve(httpServer));
- }).then(httpServer => {
- const { port } = httpServer.address();
- console.info(`==> 🌎 Listening on ${port}. Open up http://localhost:${port}/ in your browser.`);
- // Hot Module Replacement API
- if (module.hot) {
- let currentApp = app;
- module.hot.accept('./app', () => {
- httpServer.removeListener('request', currentApp);
- require('./app')
- .then(({ default: nextApp }) => {
- currentApp = nextApp;
- httpServer.on('request', currentApp);
- console.log('HttpServer reloaded!');
- })
- .catch(err => console.error(err));
- });
- // For reload main module (self). It will be restart http-server.
- module.hot.accept(err => console.error(err));
- module.hot.dispose(() => {
- console.log('Disposing entry module...');
- httpServer.close();
- });
- }
- });
- }
- console.log('Starting http server...');
- startServer().catch(err => {
- console.error('Error in server start script.', err);
- });
|