From 3bd10d605d4fe77a81c6fd0c756b5cda34fb615e Mon Sep 17 00:00:00 2001 From: Alexandre Bodin Date: Tue, 1 Sep 2020 14:59:08 +0200 Subject: [PATCH] Fix https://github.com/strapi/strapi/issues/7561 Signed-off-by: Alexandre Bodin --- packages/strapi/lib/Strapi.js | 43 +++++++++++++++++++---------------- 1 file changed, 23 insertions(+), 20 deletions(-) diff --git a/packages/strapi/lib/Strapi.js b/packages/strapi/lib/Strapi.js index 032362ea6d..53ea31e136 100644 --- a/packages/strapi/lib/Strapi.js +++ b/packages/strapi/lib/Strapi.js @@ -42,7 +42,7 @@ class Strapi { this.app = new Koa(); this.router = new Router(); - this.server = http.createServer(this.handleRequest.bind(this)); + this.initServer(); // Logger. this.log = logger; @@ -152,25 +152,8 @@ class Strapi { console.log(); } - async start(cb) { - try { - if (!this.isLoaded) { - await this.load(); - } - - this.app.use(this.router.routes()).use(this.router.allowedMethods()); - - // Launch server. - this.listen(cb); - } catch (err) { - this.stopWithError(err); - } - } - - /** - * Add behaviors to the server - */ - async listen(cb) { + initServer() { + this.server = http.createServer(this.handleRequest.bind(this)); // handle port in use cleanly this.server.on('error', err => { if (err.code === 'EADDRINUSE') { @@ -199,7 +182,27 @@ class Strapi { connections[key].destroy(); } }; + } + async start(cb) { + try { + if (!this.isLoaded) { + await this.load(); + } + + this.app.use(this.router.routes()).use(this.router.allowedMethods()); + + // Launch server. + this.listen(cb); + } catch (err) { + this.stopWithError(err); + } + } + + /** + * Add behaviors to the server + */ + async listen(cb) { const onListen = async err => { if (err) return this.stopWithError(err);