Skip to content

Commit 6de9f2b

Browse files
committed
fix: 修复逻辑顺序
1 parent b2f242b commit 6de9f2b

File tree

2 files changed

+17
-7
lines changed

2 files changed

+17
-7
lines changed

src/plugins/factory/Application.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
'use strict';
22

33
const Koa = require('koa');
4-
const { _, logger, isDocker } = require('@micro-app/shared-utils');
4+
const { _, logger, isDocker, assert } = require('@micro-app/shared-utils');
55

66
const Lifecycle = require('./Lifecycle.js');
77
const Router = require('./Router.js');
@@ -144,9 +144,14 @@ module.exports = class Application extends Koa {
144144
}
145145

146146
callback(...args) {
147+
return super.callback(...args);
148+
}
149+
150+
listen(server, ...args) {
151+
assert(server, 'server must be required');
147152
const rootRouter = this.rootRouter;
148153
this.use(rootRouter.routes());
149154
this.use(rootRouter.allowedMethods());
150-
return super.callback(...args);
155+
return server.listen(...args);
151156
}
152157
};

src/plugins/factory/index.js

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,12 @@ module.exports = async function(api, info = {}) {
3131
server = createServer(app, { protocol, isOpenHttp2: http2 });
3232
}
3333

34+
if (!server) {
35+
throw new Error('server is null');
36+
}
37+
38+
app.server = server;
39+
3440
if (entries.length > 0) {
3541
await entries.reduce((chain, entry) => {
3642
const runApp = require(entry); // app.js
@@ -51,7 +57,7 @@ module.exports = async function(api, info = {}) {
5157
});
5258
const _host = process.env.HOST || apiContext.host || host || '0.0.0.0';
5359

54-
const ps = [ listen(server, { protocol, host: _host, port: _port }) ];
60+
const ps = [ listen(app, { protocol, host: _host, port: _port }) ];
5561
return Promise.all(ps).then(ress => {
5662
const res = { ...ress[0] };
5763
return res; // 只返回第一个配置
@@ -89,11 +95,10 @@ function createServer(app, { protocol, isOpenHttp2 }, options) {
8995
throw new Error(`Not Support protocol: ${protocol}!`);
9096
}
9197

92-
app.server = server;
9398
return server;
9499
}
95100

96-
function listen(server, { protocol, host, port }) {
101+
function listen(app, { protocol, host, port }) {
97102
return new Promise((resolve, reject) => {
98103
const errCb = err => {
99104
if (err) {
@@ -106,8 +111,8 @@ function listen(server, { protocol, host, port }) {
106111
}
107112
};
108113

109-
if (server) {
110-
server.listen(port, errCb);
114+
if (app) {
115+
app.listen(app.server, port, errCb);
111116
} else {
112117
reject(new Error(`Not Support protocol: ${protocol}!`));
113118
}

0 commit comments

Comments
 (0)