1
- import express , { Request , Response , NextFunction } from "express" ;
2
- import { Container } from "inversify" ;
3
- import { provide } from "inversify-binding-decorators" ;
4
- import process from "process" ;
5
1
import {
6
2
Console ,
7
3
IApplicationMessageToConsole ,
8
- IMiddleware ,
9
- Middleware ,
10
- Logger ,
11
4
IHandlebars ,
5
+ Logger ,
6
+ Middleware ,
12
7
RenderTemplateOptions ,
13
8
} from "@expressots/core" ;
14
- import { IApplicationExpress } from "./application-express.interface" ;
9
+ import express from "express" ;
10
+ import { Container } from "inversify" ;
11
+ import { provide } from "inversify-binding-decorators" ;
12
+ import process from "process" ;
13
+ import { ApplicationBase } from "./application-express.base" ;
15
14
import { InversifyExpressServer } from "./express-utils/inversify-express-server" ;
16
- import { ApplicationBase } from "./application-base" ;
17
-
18
- /**
19
- * ExpressHandler Type
20
- *
21
- * The ExpressHandler type is a union type that represents various types of Express middleware functions.
22
- * It can be one of the following types:
23
- * - express.ErrorRequestHandler: Handles errors in the middleware pipeline.
24
- * - express.RequestParamHandler: Handles parameters in the middleware pipeline.
25
- * - express.RequestHandler: General request handler.
26
- * - undefined: Represents the absence of a handler.
27
- */
28
- type ExpressHandler =
29
- | express . ErrorRequestHandler
30
- | express . RequestParamHandler
31
- | express . RequestHandler
32
- | undefined ;
33
-
34
- /**
35
- * MiddlewareConfig Interface
36
- *
37
- * The MiddlewareConfig interface specifies the structure for middleware configuration objects.
38
- * - path: Optional. The route path for which the middleware is configured.
39
- * - middlewares: An array of ExpressHandler types that make up the middleware pipeline for the route specified by 'path'.
40
- */
41
- type MiddlewareConfig = {
42
- path ?: string ;
43
- middlewares : Array < ExpressHandler > ;
44
- } ;
45
-
46
- /**
47
- * Expresso middleware interface.
48
- */
49
- interface IExpressoMiddleware {
50
- //readonly name: string;
51
- use ( req : Request , res : Response , next : NextFunction ) : Promise < void > | void ;
52
- }
53
-
54
- /**
55
- * Abstract class for creating custom Expresso middleware.
56
- * Custom middleware classes should extend this class and implement the use method.
57
- *
58
- */
59
- abstract class ExpressoMiddleware implements IExpressoMiddleware {
60
- get name ( ) : string {
61
- return this . constructor . name ;
62
- }
63
-
64
- abstract use ( req : Request , res : Response , next : NextFunction ) : Promise < void > | void ;
65
- }
66
-
67
- /**
68
- * Enum representing possible server environments.
69
- */
70
- enum ServerEnvironment {
71
- Development = "development" ,
72
- Production = "production" ,
73
- }
15
+ import {
16
+ ExpressHandler ,
17
+ ExpressoMiddleware ,
18
+ IWebServer ,
19
+ MiddlewareConfig ,
20
+ ServerEnvironment ,
21
+ } from "./application-express.types" ;
74
22
75
23
/**
76
- * The Application class provides a way to configure and manage an Express application.
77
- * @provide Application
24
+ * The AppExpress class provides methods for configuring and running an Express application.
25
+ * @class AppExpress
26
+ * @implements {IWebServer} - Interface for the WebServer application implementation.
27
+ * @extends {ApplicationBase } - Base class for the application implementation that provides lifecycle hooks.
28
+ * @method configure - Configures the InversifyJS container.
29
+ * @method listen - Start listening on the given port and environment.
30
+ * @method setGlobalRoutePrefix - Sets the global route prefix for the application.
31
+ * @method setEngine - Configures the application's view engine based on the provided configuration options.
32
+ * @method isDevelopment - Verifies if the current environment is development.
78
33
*/
79
- @provide ( ApplicationExpress )
80
- class ApplicationExpress extends ApplicationBase implements IApplicationExpress {
34
+ @provide ( AppExpress )
35
+ class AppExpress extends ApplicationBase implements IWebServer {
81
36
private app : express . Application ;
82
37
private port : number ;
83
38
private environment : ServerEnvironment ;
84
39
private container : Container ;
85
- private middlewares : Array < ExpressHandler > = [ ] ;
86
- private globalPrefix : string | undefined ;
40
+ private globalPrefix : string = "/" ;
41
+ private middlewares : Array < ExpressHandler | MiddlewareConfig | ExpressoMiddleware > = [ ] ;
42
+ private console : Console ;
87
43
88
44
protected configureServices ( ) : void | Promise < void > { }
89
45
protected postServerInitialization ( ) : void | Promise < void > { }
@@ -97,6 +53,15 @@ class ApplicationExpress extends ApplicationBase implements IApplicationExpress
97
53
process . exit ( 0 ) ;
98
54
}
99
55
56
+ /**
57
+ * Configures the InversifyJS container.
58
+ * @param container - The InversifyJS container.
59
+ */
60
+ async configure ( container : Container ) : Promise < void > {
61
+ this . container = container ;
62
+ this . console = this . container . get ( Console ) ;
63
+ }
64
+
100
65
/**
101
66
* Configures the Express application with the provided middleware entries.
102
67
* @param app - The Express application instance.
@@ -137,19 +102,16 @@ class ApplicationExpress extends ApplicationBase implements IApplicationExpress
137
102
* @param middlewares - An array of Express middlewares to be applied.
138
103
* @returns The configured Application instance.
139
104
*/
140
- private async init (
141
- container : Container ,
142
- middlewares : Array < express . RequestHandler > = [ ] ,
143
- ) : Promise < ApplicationExpress > {
105
+ private async init ( ) : Promise < AppExpress > {
144
106
await this . configureServices ( ) ;
145
107
146
- const middleware = container . get < IMiddleware > ( Middleware ) ;
108
+ const middleware = this . container . get ( Middleware ) ;
147
109
const sortedMiddlewarePipeline = middleware . getMiddlewarePipeline ( ) ;
148
110
const pipeline = sortedMiddlewarePipeline . map ( ( entry ) => entry . middleware ) ;
149
111
150
- this . middlewares . push ( ...middlewares , ... ( pipeline as Array < ExpressHandler > ) ) ;
112
+ this . middlewares . push ( ...( pipeline as Array < ExpressHandler > ) ) ;
151
113
152
- const expressServer = new InversifyExpressServer ( container , null , {
114
+ const expressServer = new InversifyExpressServer ( this . container , null , {
153
115
rootPath : this . globalPrefix as string ,
154
116
} ) ;
155
117
@@ -167,23 +129,6 @@ class ApplicationExpress extends ApplicationBase implements IApplicationExpress
167
129
return this ;
168
130
}
169
131
170
- /**
171
- * Create and configure the Express application.
172
- * @param container - The InversifyJS container.
173
- * @param middlewares - An array of Express middlewares to be applied.
174
- * @returns The configured Application instance.
175
- */
176
- public async create (
177
- container : Container ,
178
- middlewares : Array < ExpressHandler > = [ ] ,
179
- ) : Promise < ApplicationExpress > {
180
- this . container = container ;
181
- this . middlewares = middlewares ;
182
- this . globalPrefix = this . globalPrefix || "/" ;
183
-
184
- return this ;
185
- }
186
-
187
132
/**
188
133
* Start listening on the given port and environment.
189
134
* @param port - The port number to listen on.
@@ -195,17 +140,14 @@ class ApplicationExpress extends ApplicationBase implements IApplicationExpress
195
140
environment : ServerEnvironment ,
196
141
consoleMessage ?: IApplicationMessageToConsole ,
197
142
) : Promise < void > {
198
- /* Initializes the application and executes the middleware pipeline */
199
- await this . init ( this . container , this . middlewares as Array < express . RequestHandler > ) ;
143
+ await this . init ( ) ;
200
144
201
- /* Sets the port and environment */
202
145
this . port = port ;
203
146
this . environment = environment ;
204
147
this . app . set ( "env" , environment ) ;
205
148
206
149
this . app . listen ( this . port , ( ) => {
207
- const console : Console = this . container . get < Console > ( Console ) ;
208
- console . messageServer ( this . port , this . environment , consoleMessage ) ;
150
+ this . console . messageServer ( this . port , this . environment , consoleMessage ) ;
209
151
210
152
( [ "SIGTERM" , "SIGHUP" , "SIGBREAK" , "SIGQUIT" , "SIGINT" ] as Array < NodeJS . Signals > ) . forEach (
211
153
( signal ) => {
@@ -214,7 +156,7 @@ class ApplicationExpress extends ApplicationBase implements IApplicationExpress
214
156
) ;
215
157
} ) ;
216
158
217
- await Promise . resolve ( this . postServerInitialization ( ) ) ;
159
+ await this . postServerInitialization ( ) ;
218
160
}
219
161
220
162
/**
@@ -265,16 +207,6 @@ class ApplicationExpress extends ApplicationBase implements IApplicationExpress
265
207
. error ( "isDevelopment() method must be called on `PostServerInitialization`" , "application" ) ;
266
208
return false ;
267
209
}
268
-
269
- /**
270
- * Verifies if the current environment is production.
271
- *
272
- * @returns A boolean value indicating whether the current environment is production or not.
273
- *
274
- */
275
- public get ExpressApp ( ) : express . Application {
276
- return this . app ;
277
- }
278
210
}
279
211
280
- export { ApplicationExpress as AppExpress , ServerEnvironment } ;
212
+ export { AppExpress } ;
0 commit comments