/
index.js
113 lines (101 loc) · 3.25 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
var fs = require("fs");
/**
* @summary
* Exported class to handle directory requests.
*
* When there is first request to directory with default
* `index.js` script inside, this class is automatically
* created and method `Start()` is executed.
* All request are normally handled by method `HttpHandle()`.
* If there is detected any file change inside this file
* or inside file included in this file (on development server
* instance), the module `web-dev-server` automaticly reloads
* all necesssary dependent source codes, stops previous instance
* by method `Stop`() and recreates this application instance again
* by `Start()` method. The same realoding procedure is executed,
* if there is any unhandled error inside method `HttpHandle()`
* (to develop more comfortably).
*/
module.exports = class App {
constructor () {
/**
* @summary WebDevServer server instance.
* @var {WebDevServer.Server}
*/
this.server = null;
/**
* @summary Requests counter.
* @var {number}
*/
this.counter = 0;
}
/**
* @summary Application start point.
* @public
* @param {WebDevServer.Server} server
* @param {WebDevServer.Request} firstRequest
* @param {WebDevServer.Response} firstResponse
* @return {Promise<void>}
*/
async Start (server, firstRequest, firstResponse) {
this.server = server;
// Any initializations:
console.log("App start.");
}
/**
* @summary Application end point, called on unhandled error
* (on development server instance) or on server stop event.
* @public
* @param {WebDevServer.Server} server
* @return {Promise<void>}
*/
async Stop (server) {
// Any destructions:
console.log("App stop.");
}
/**
* @summary
* This method is executed each request to directory with
* `index.js` script inside (also executed for first time
* immediately after `Start()` method).
* @public
* @param {WebDevServer.Request} request
* @param {WebDevServer.Response} response
* @return {Promise<void>}
*/
async HttpHandle (request, response) {
console.log("App http handle.", request.GetFullUrl());
// increase request counter:
this.counter++;
// try to uncomment line bellow to see rendered error in browser:
//throw new Error("Uncatched test error 1.");
if (!request.IsCompleted()) await request.GetBody();
// say hallo world with html template:
var staticHtmlFileFullPath = this.server.GetDocumentRoot() + '/static-content/index.html';
//var data = await fs.promises.readFile(staticHtmlFileFullPath, 'utf8'); // experimental
var data = await new Promise(function(resolve, reject) {
fs.readFile(staticHtmlFileFullPath, 'utf8', function (err, data) {
// try to uncomment line bellow to see rendered error in browser:
try {
//throw new Error("Uncatched test error 2.");
} catch (e) {
err = e;
}
if (err) return reject(err);
resolve(data);
});
});
response.SetBody(data.replace(
/%code%/g, JSON.stringify({
basePath: request.GetBasePath(),
path: request.GetPath(),
domainUrl: request.GetDomainUrl(),
baseUrl: request.GetBaseUrl(),
requestUrl: request.GetRequestUrl(),
fullUrl: request.GetFullUrl(),
params: request.GetParams(false, false),
appRequests: this.counter
}, null, "\t")
)).Send();
}
}