@midwayjs/express 4.0.0-beta.1 → 4.0.0-beta.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2013 - Now midwayjs
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/dist/framework.js CHANGED
@@ -139,18 +139,27 @@ let MidwayExpressFramework = class MidwayExpressFramework extends core_1.BaseFra
139
139
  }
140
140
  // register httpServer to applicationContext
141
141
  this.applicationContext.registerObject(core_1.HTTP_SERVER_KEY, this.server);
142
- const customPort = process.env.MIDWAY_HTTP_PORT ?? this.configurationOptions.port;
143
- if (customPort) {
142
+ this.configurationOptions.listenOptions = {
143
+ port: this.configurationOptions.port,
144
+ host: this.configurationOptions.hostname,
145
+ ...this.configurationOptions.listenOptions,
146
+ };
147
+ // set port and listen server
148
+ let customPort = process.env.MIDWAY_HTTP_PORT ||
149
+ this.configurationOptions.listenOptions.port;
150
+ if (customPort === 0 || customPort === '0') {
151
+ customPort = await (0, util_2.getFreePort)();
152
+ this.logger.info(`[midway:express] detect available port: ${customPort}`);
153
+ }
154
+ this.configurationOptions.listenOptions.port = Number(customPort);
155
+ if (this.configurationOptions.listenOptions.port) {
144
156
  new Promise(resolve => {
145
- const args = [customPort];
146
- if (this.configurationOptions.hostname) {
147
- args.push(this.configurationOptions.hostname);
148
- }
149
- args.push(() => {
157
+ // 使用 ListenOptions 对象启动服务器
158
+ this.server.listen(this.configurationOptions.listenOptions, () => {
150
159
  resolve();
151
160
  });
152
- this.server.listen(...args);
153
- process.env.MIDWAY_HTTP_PORT = String(customPort);
161
+ process.env.MIDWAY_HTTP_PORT = String(this.configurationOptions.listenOptions.port);
162
+ this.logger.debug(`[midway:express] Server listening on http://${this.configurationOptions.hostname || 'localhost'}:${customPort}`);
154
163
  });
155
164
  }
156
165
  }
@@ -221,7 +230,7 @@ let MidwayExpressFramework = class MidwayExpressFramework extends core_1.BaseFra
221
230
  for (const routerInfo of routerList) {
222
231
  // bind controller first
223
232
  this.getApplicationContext().bindClass(routerInfo.routerModule);
224
- this.logger.debug(`Load Controller "${routerInfo.controllerId}", prefix=${routerInfo.prefix}`);
233
+ this.logger.debug(`[midway:express] Load Controller "${routerInfo.controllerId}", prefix=${routerInfo.prefix}`);
225
234
  // new router
226
235
  const newRouter = this.createRouter(routerInfo.routerOptions);
227
236
  routerInfo.middleware = routerInfo.middleware ?? [];
@@ -240,9 +249,9 @@ let MidwayExpressFramework = class MidwayExpressFramework extends core_1.BaseFra
240
249
  const routeMiddlewareFn = await this.expressMiddlewareService.compose(routeInfo.middleware, this.app);
241
250
  routeMiddlewareList.push(routeMiddlewareFn);
242
251
  }
243
- this.logger.debug(`Load Router "${routeInfo.requestMethod.toUpperCase()} ${routeInfo.url}"`);
252
+ this.logger.debug(`[midway:express] Load Router "${routeInfo.requestMethod.toUpperCase()} ${routeInfo.url}"`);
244
253
  // apply controller from request context
245
- newRouter[routeInfo.requestMethod].call(newRouter, routeInfo.url, ...routeMiddlewareList, this.generateController(routeInfo));
254
+ newRouter[routeInfo.requestMethod.toLowerCase()].call(newRouter, routeInfo.url, ...routeMiddlewareList, this.generateController(routeInfo));
246
255
  }
247
256
  routerMiddlewares.push({
248
257
  prefix: routerInfo.prefix,
@@ -271,7 +280,9 @@ let MidwayExpressFramework = class MidwayExpressFramework extends core_1.BaseFra
271
280
  if (this.server) {
272
281
  new Promise(resolve => {
273
282
  this.server.close(resolve);
283
+ process.env.MIDWAY_HTTP_PORT = '';
274
284
  });
285
+ this.logger.debug('[midway:express] server close');
275
286
  }
276
287
  }
277
288
  getServer() {
@@ -1,7 +1,9 @@
1
1
  /// <reference types="node" />
2
2
  /// <reference types="node" />
3
+ /// <reference types="node" />
3
4
  import { CommonMiddlewareUnion, ContextMiddlewareManager, FunctionMiddleware, IConfigurationOptions, IMiddleware, IMidwayApplication, IMidwayContext } from '@midwayjs/core';
4
5
  import { Application as ExpressApplication, NextFunction as ExpressNextFunction, Request as ExpressRequest, Response as ExpressResponse } from 'express';
6
+ import { ListenOptions } from 'net';
5
7
  type Request = IMidwayContext<ExpressRequest>;
6
8
  export type Response = ExpressResponse;
7
9
  export type NextFunction = ExpressNextFunction;
@@ -70,6 +72,10 @@ export interface IMidwayExpressConfigurationOptions extends IConfigurationOption
70
72
  * https/https/http2 server options
71
73
  */
72
74
  serverOptions?: Record<string, any>;
75
+ /**
76
+ * listen options
77
+ */
78
+ listenOptions?: ListenOptions;
73
79
  }
74
80
  export type Application = IMidwayExpressApplication;
75
81
  export {};
package/dist/util.d.ts CHANGED
@@ -1,2 +1,3 @@
1
1
  export declare function sendData(res: any, data: any): void;
2
+ export declare function getFreePort(): Promise<number>;
2
3
  //# sourceMappingURL=util.d.ts.map
package/dist/util.js CHANGED
@@ -1,6 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.sendData = void 0;
3
+ exports.getFreePort = exports.sendData = void 0;
4
+ const net_1 = require("net");
4
5
  function sendData(res, data) {
5
6
  if (typeof data === 'number') {
6
7
  res.status(res.statusCode).send('' + data);
@@ -10,4 +11,20 @@ function sendData(res, data) {
10
11
  }
11
12
  }
12
13
  exports.sendData = sendData;
14
+ async function getFreePort() {
15
+ return new Promise((resolve, reject) => {
16
+ const server = (0, net_1.createServer)();
17
+ server.listen(0, () => {
18
+ try {
19
+ const port = server.address().port;
20
+ server.close();
21
+ resolve(port);
22
+ }
23
+ catch (err) {
24
+ reject(err);
25
+ }
26
+ });
27
+ });
28
+ }
29
+ exports.getFreePort = getFreePort;
13
30
  //# sourceMappingURL=util.js.map
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@midwayjs/express",
3
- "version": "4.0.0-beta.1",
3
+ "version": "4.0.0-beta.2",
4
4
  "description": "Midway Web Framework for Express",
5
5
  "main": "dist/index.js",
6
6
  "typings": "index.d.ts",
@@ -24,14 +24,14 @@
24
24
  ],
25
25
  "license": "MIT",
26
26
  "devDependencies": {
27
- "@midwayjs/core": "^4.0.0-beta.1",
28
- "@midwayjs/mock": "^4.0.0-beta.1",
27
+ "@midwayjs/core": "^4.0.0-beta.2",
28
+ "@midwayjs/mock": "^4.0.0-beta.2",
29
29
  "@types/body-parser": "1.19.5",
30
- "@types/express": "4.17.21",
30
+ "@types/express": "4.17.23",
31
31
  "fs-extra": "11.3.0"
32
32
  },
33
33
  "dependencies": {
34
- "@midwayjs/express-session": "^4.0.0-beta.1",
34
+ "@midwayjs/express-session": "^4.0.0-beta.2",
35
35
  "body-parser": "1.20.3",
36
36
  "cookie-parser": "^1.4.6",
37
37
  "express": "4.21.2"
@@ -42,7 +42,7 @@
42
42
  "url": "https://github.com/midwayjs/midway.git"
43
43
  },
44
44
  "engines": {
45
- "node": ">=12"
45
+ "node": ">=20"
46
46
  },
47
- "gitHead": "832961ec3aff123c033197d8c00cb2bc9bad7ff8"
47
+ "gitHead": "53bfef4c5279da5f09025e4610bdbf64f94f60bd"
48
48
  }