Signed-off-by: Alexandre Bodin <bodin.alex@gmail.com>
This commit is contained in:
Alexandre Bodin 2020-09-01 14:59:08 +02:00
parent 77be3ea4c5
commit 3bd10d605d

View File

@ -42,7 +42,7 @@ class Strapi {
this.app = new Koa(); this.app = new Koa();
this.router = new Router(); this.router = new Router();
this.server = http.createServer(this.handleRequest.bind(this)); this.initServer();
// Logger. // Logger.
this.log = logger; this.log = logger;
@ -152,25 +152,8 @@ class Strapi {
console.log(); console.log();
} }
async start(cb) { initServer() {
try { this.server = http.createServer(this.handleRequest.bind(this));
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) {
// handle port in use cleanly // handle port in use cleanly
this.server.on('error', err => { this.server.on('error', err => {
if (err.code === 'EADDRINUSE') { if (err.code === 'EADDRINUSE') {
@ -199,7 +182,27 @@ class Strapi {
connections[key].destroy(); 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 => { const onListen = async err => {
if (err) return this.stopWithError(err); if (err) return this.stopWithError(err);