@webtypen/webframez-core 0.0.11 → 0.0.13
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/lib/Router/Router.d.ts +5 -1
- package/lib/Router/Router.js +16 -7
- package/lib/WebApplication.d.ts +1 -1
- package/lib/WebApplication.js +4 -3
- package/package.json +1 -1
- package/src/Router/Router.ts +30 -19
- package/src/WebApplication.ts +4 -3
package/lib/Router/Router.d.ts
CHANGED
|
@@ -9,6 +9,10 @@ type RouteObject = {
|
|
|
9
9
|
params: object;
|
|
10
10
|
};
|
|
11
11
|
declare class RouterFacade {
|
|
12
|
+
/**
|
|
13
|
+
* Basename
|
|
14
|
+
*/
|
|
15
|
+
basename: string | null;
|
|
12
16
|
/**
|
|
13
17
|
* GET Route Store
|
|
14
18
|
*/
|
|
@@ -57,7 +61,7 @@ declare class RouterFacade {
|
|
|
57
61
|
* Load the application-routes
|
|
58
62
|
*
|
|
59
63
|
*/
|
|
60
|
-
init(): void;
|
|
64
|
+
init(options?: any): void;
|
|
61
65
|
/**
|
|
62
66
|
* Register a route
|
|
63
67
|
*
|
package/lib/Router/Router.js
CHANGED
|
@@ -19,6 +19,10 @@ const Response_1 = require("./Response");
|
|
|
19
19
|
const Kernel_1 = require("../../../../../app/Kernel");
|
|
20
20
|
class RouterFacade {
|
|
21
21
|
constructor() {
|
|
22
|
+
/**
|
|
23
|
+
* Basename
|
|
24
|
+
*/
|
|
25
|
+
this.basename = null;
|
|
22
26
|
/**
|
|
23
27
|
* GET Route Store
|
|
24
28
|
*/
|
|
@@ -40,7 +44,8 @@ class RouterFacade {
|
|
|
40
44
|
* Load the application-routes
|
|
41
45
|
*
|
|
42
46
|
*/
|
|
43
|
-
init() {
|
|
47
|
+
init(options) {
|
|
48
|
+
this.basename = options && options.basename ? options.basename : null;
|
|
44
49
|
// Load routes
|
|
45
50
|
require(process.cwd() + "/app/routes");
|
|
46
51
|
}
|
|
@@ -53,6 +58,9 @@ class RouterFacade {
|
|
|
53
58
|
* @param options
|
|
54
59
|
*/
|
|
55
60
|
register(type, path, component, options) {
|
|
61
|
+
if (this.basename) {
|
|
62
|
+
path = this.basename + path;
|
|
63
|
+
}
|
|
56
64
|
if (type === "GET") {
|
|
57
65
|
this.routesGET[path] = {
|
|
58
66
|
path: path,
|
|
@@ -91,16 +99,16 @@ class RouterFacade {
|
|
|
91
99
|
return this.dissolveRoute(this.routesDELETE, req.url);
|
|
92
100
|
}
|
|
93
101
|
else if (req.method === "OPTIONS") {
|
|
94
|
-
if (req.headers[
|
|
102
|
+
if (req.headers["access-control-request-method"] === "GET") {
|
|
95
103
|
return this.dissolveRoute(this.routesGET, req.url);
|
|
96
104
|
}
|
|
97
|
-
else if (req.headers[
|
|
105
|
+
else if (req.headers["access-control-request-method"] === "POST") {
|
|
98
106
|
return this.dissolveRoute(this.routesPOST, req.url);
|
|
99
107
|
}
|
|
100
|
-
else if (req.headers[
|
|
108
|
+
else if (req.headers["access-control-request-method"] === "PUT") {
|
|
101
109
|
return this.dissolveRoute(this.routesPUT, req.url);
|
|
102
110
|
}
|
|
103
|
-
else if (req.headers[
|
|
111
|
+
else if (req.headers["access-control-request-method"] === "DELETE") {
|
|
104
112
|
return this.dissolveRoute(this.routesDELETE, req.url);
|
|
105
113
|
}
|
|
106
114
|
}
|
|
@@ -206,7 +214,7 @@ class RouterFacade {
|
|
|
206
214
|
// Handle OPTIONS-Request
|
|
207
215
|
if (req.method === "OPTIONS") {
|
|
208
216
|
res.statusCode = 200;
|
|
209
|
-
res.setHeader(
|
|
217
|
+
res.setHeader("Content-Length", "0");
|
|
210
218
|
res.end();
|
|
211
219
|
return;
|
|
212
220
|
}
|
|
@@ -238,7 +246,8 @@ class RouterFacade {
|
|
|
238
246
|
yield route.component({
|
|
239
247
|
body: bodyPlain &&
|
|
240
248
|
req.headers["content-type"] === "application/json" &&
|
|
241
|
-
(bodyPlain.trim().substring(0, 1) === "[" ||
|
|
249
|
+
(bodyPlain.trim().substring(0, 1) === "[" ||
|
|
250
|
+
bodyPlain.trim().substring(0, 1) === "{")
|
|
242
251
|
? JSON.parse(bodyPlain)
|
|
243
252
|
: bodyPlain
|
|
244
253
|
? qs_1.default.parse(bodyPlain)
|
package/lib/WebApplication.d.ts
CHANGED
package/lib/WebApplication.js
CHANGED
|
@@ -10,14 +10,15 @@ class WebApplication {
|
|
|
10
10
|
/**
|
|
11
11
|
* Init the routes and start the http-server
|
|
12
12
|
*/
|
|
13
|
-
boot() {
|
|
13
|
+
boot(options) {
|
|
14
14
|
Router_1.Router.init();
|
|
15
|
+
const port = options && options.port ? options.port : 3000;
|
|
15
16
|
this.server = http_1.default
|
|
16
17
|
.createServer((req, res) => {
|
|
17
18
|
Router_1.Router.handleRequest(req, res);
|
|
18
19
|
})
|
|
19
|
-
.listen(
|
|
20
|
-
console.log("Server started and listening on port
|
|
20
|
+
.listen(port, () => {
|
|
21
|
+
console.log("Server started and listening on port " + port);
|
|
21
22
|
});
|
|
22
23
|
}
|
|
23
24
|
}
|
package/package.json
CHANGED
package/src/Router/Router.ts
CHANGED
|
@@ -12,6 +12,11 @@ type RouteObject = {
|
|
|
12
12
|
};
|
|
13
13
|
|
|
14
14
|
class RouterFacade {
|
|
15
|
+
/**
|
|
16
|
+
* Basename
|
|
17
|
+
*/
|
|
18
|
+
basename: string | null = null;
|
|
19
|
+
|
|
15
20
|
/**
|
|
16
21
|
* GET Route Store
|
|
17
22
|
*/
|
|
@@ -64,7 +69,9 @@ class RouterFacade {
|
|
|
64
69
|
* Load the application-routes
|
|
65
70
|
*
|
|
66
71
|
*/
|
|
67
|
-
init() {
|
|
72
|
+
init(options?: any) {
|
|
73
|
+
this.basename = options && options.basename ? options.basename : null;
|
|
74
|
+
|
|
68
75
|
// Load routes
|
|
69
76
|
require(process.cwd() + "/app/routes");
|
|
70
77
|
}
|
|
@@ -78,6 +85,10 @@ class RouterFacade {
|
|
|
78
85
|
* @param options
|
|
79
86
|
*/
|
|
80
87
|
register(type: string, path: string, component: any, options: object) {
|
|
88
|
+
if (this.basename) {
|
|
89
|
+
path = this.basename + path;
|
|
90
|
+
}
|
|
91
|
+
|
|
81
92
|
if (type === "GET") {
|
|
82
93
|
this.routesGET[path] = {
|
|
83
94
|
path: path,
|
|
@@ -113,13 +124,13 @@ class RouterFacade {
|
|
|
113
124
|
} else if (req.method === "DELETE") {
|
|
114
125
|
return this.dissolveRoute(this.routesDELETE, req.url);
|
|
115
126
|
} else if (req.method === "OPTIONS") {
|
|
116
|
-
if (req.headers[
|
|
127
|
+
if (req.headers["access-control-request-method"] === "GET") {
|
|
117
128
|
return this.dissolveRoute(this.routesGET, req.url);
|
|
118
|
-
} else if(req.headers[
|
|
129
|
+
} else if (req.headers["access-control-request-method"] === "POST") {
|
|
119
130
|
return this.dissolveRoute(this.routesPOST, req.url);
|
|
120
|
-
} else if(req.headers[
|
|
131
|
+
} else if (req.headers["access-control-request-method"] === "PUT") {
|
|
121
132
|
return this.dissolveRoute(this.routesPUT, req.url);
|
|
122
|
-
} else if(req.headers[
|
|
133
|
+
} else if (req.headers["access-control-request-method"] === "DELETE") {
|
|
123
134
|
return this.dissolveRoute(this.routesDELETE, req.url);
|
|
124
135
|
}
|
|
125
136
|
}
|
|
@@ -189,10 +200,10 @@ class RouterFacade {
|
|
|
189
200
|
* @param res
|
|
190
201
|
*/
|
|
191
202
|
async handleRequest(req: IncomingMessage, res: ServerResponse) {
|
|
192
|
-
const route = this.dissolve(req);
|
|
203
|
+
const route = this.dissolve(req);
|
|
193
204
|
const response = new Response();
|
|
194
205
|
response.setServerResponse(res);
|
|
195
|
-
|
|
206
|
+
|
|
196
207
|
// Check-Middleware
|
|
197
208
|
if (
|
|
198
209
|
route &&
|
|
@@ -210,7 +221,6 @@ class RouterFacade {
|
|
|
210
221
|
);
|
|
211
222
|
}
|
|
212
223
|
|
|
213
|
-
|
|
214
224
|
try {
|
|
215
225
|
await new Promise((resolve, reject) => {
|
|
216
226
|
try {
|
|
@@ -254,10 +264,10 @@ class RouterFacade {
|
|
|
254
264
|
|
|
255
265
|
// Handle OPTIONS-Request
|
|
256
266
|
if (req.method === "OPTIONS") {
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
267
|
+
res.statusCode = 200;
|
|
268
|
+
res.setHeader("Content-Length", "0");
|
|
269
|
+
res.end();
|
|
270
|
+
return;
|
|
261
271
|
}
|
|
262
272
|
|
|
263
273
|
// Resolve route-component (handle controller definition)
|
|
@@ -291,13 +301,14 @@ class RouterFacade {
|
|
|
291
301
|
await route.component(
|
|
292
302
|
{
|
|
293
303
|
body:
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
304
|
+
bodyPlain &&
|
|
305
|
+
req.headers["content-type"] === "application/json" &&
|
|
306
|
+
(bodyPlain.trim().substring(0, 1) === "[" ||
|
|
307
|
+
bodyPlain.trim().substring(0, 1) === "{")
|
|
308
|
+
? JSON.parse(bodyPlain)
|
|
309
|
+
: bodyPlain
|
|
310
|
+
? qs.parse(bodyPlain)
|
|
311
|
+
: {},
|
|
301
312
|
bodyPlain: bodyPlain,
|
|
302
313
|
params: route.params,
|
|
303
314
|
httpVersionMajor: req.httpVersionMajor,
|
package/src/WebApplication.ts
CHANGED
|
@@ -7,15 +7,16 @@ export class WebApplication {
|
|
|
7
7
|
/**
|
|
8
8
|
* Init the routes and start the http-server
|
|
9
9
|
*/
|
|
10
|
-
boot() {
|
|
10
|
+
boot(options?: any) {
|
|
11
11
|
Router.init();
|
|
12
12
|
|
|
13
|
+
const port = options && options.port ? options.port : 3000;
|
|
13
14
|
this.server = http
|
|
14
15
|
.createServer((req, res) => {
|
|
15
16
|
Router.handleRequest(req, res);
|
|
16
17
|
})
|
|
17
|
-
.listen(
|
|
18
|
-
console.log("Server started and listening on port
|
|
18
|
+
.listen(port, () => {
|
|
19
|
+
console.log("Server started and listening on port " + port);
|
|
19
20
|
});
|
|
20
21
|
}
|
|
21
22
|
}
|