diff --git a/packages/strapi/lib/Strapi.js b/packages/strapi/lib/Strapi.js index 4af4f114bb..dac47087b0 100755 --- a/packages/strapi/lib/Strapi.js +++ b/packages/strapi/lib/Strapi.js @@ -87,6 +87,9 @@ class Strapi extends EventEmitter { try { this.config = assign(this.config, config) + // Emit starting event. + this.emit('server:starting'); + // Enhance app. await this.enhancer(); // Load the app. @@ -100,7 +103,9 @@ class Strapi extends EventEmitter { // Launch server. this.server.listen(this.config.port, err => { if (err) { - console.log(err); + this.log.debug(`Server wasn't able to start properly.`); + console.error(err); + return this.stop(); } this.log.info('Server started in ' + this.config.appPath); @@ -112,14 +117,16 @@ class Strapi extends EventEmitter { this.log.debug(`Version: ${this.config.info.strapi} (node v${this.config.info.node})`); this.log.info('To shut down your server, press + C at any time'); + // Emit started event. + this.emit('server:started'); + if (cb && typeof cb === 'function') { cb(); } }); - } catch (e) { + } catch (err) { this.log.debug(`Server wasn't able to start properly.`); - this.log.error(e); - console.error(e); + console.error(err); this.stop(); } } @@ -136,6 +143,17 @@ class Strapi extends EventEmitter { }); }); + this.server.on('error', err => { + if (err.code === 'EADDRINUSE') { + this.log.debug(`Server wasn't able to start properly.`); + this.log.error(`The port ${err.port} is already used by another application.`); + this.stop(); + return; + } + + console.error(err); + }); + this.server.destroy = cb => { this.server.close(cb); diff --git a/packages/strapi/lib/core/hooks.js b/packages/strapi/lib/core/hooks.js index ac1a4a79c7..847ac72b64 100755 --- a/packages/strapi/lib/core/hooks.js +++ b/packages/strapi/lib/core/hooks.js @@ -91,6 +91,12 @@ const mountHooks = function (files, cwd) { }); }), - resolve + (err, results) => { + if (err) { + return reject(err); + } + + resolve(); + } ); }; diff --git a/packages/strapi/lib/utils/index.js b/packages/strapi/lib/utils/index.js index 51aec60ed2..1be9480652 100755 --- a/packages/strapi/lib/utils/index.js +++ b/packages/strapi/lib/utils/index.js @@ -14,16 +14,10 @@ const exposer = require('./exposer'); module.exports = { loadFile: function(url) { - try { - // Clear cache. - delete require.cache[require.resolve(path.resolve(this.config.appPath, url))]; - // Require without cache. - return require(path.resolve(this.config.appPath, url)); - } catch (e) { - this.log.error(e); - - return {}; - } + // Clear cache. + delete require.cache[require.resolve(path.resolve(this.config.appPath, url))]; + // Require without cache. + return require(path.resolve(this.config.appPath, url)); }, setConfig: function(ctx, path, type, loader) { @@ -123,11 +117,12 @@ module.exports = { try { if (this.config.uuid) { const publicKey = fs.readFileSync(path.resolve(__dirname, 'resources', 'key.pub')); + const options = { timeout: 1000 }; const [usage, signedHash, required] = await Promise.all([ - fetch('https://strapi.io/assets/images/usage.gif'), - fetch('https://strapi.io/hash.txt'), - fetch('https://strapi.io/required.txt') + fetch('https://strapi.io/assets/images/usage.gif', options), + fetch('https://strapi.io/hash.txt', options), + fetch('https://strapi.io/required.txt', options) ]); if (usage.status === 200 && signedHash.status === 200) {