-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathexpress-app.html
105 lines (90 loc) · 3.13 KB
/
express-app.html
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
<script>
class ExpressApp extends HTMLElement {
constructor() {
super();
this._port = null;
this.path = null;
this.hostname = null;
this.backlog = null;
}
static get observedAttributes() {
return [
'port',
'path',
'hostname',
'backlog'
];
}
attributeChangedCallback(name, oldValue, newValue) {
switch (name) {
case 'port': {
this.port = newValue;
break;
}
case 'path': {
this.path = newValue;
break;
}
case 'hostname': {
this.hostname = newValue;
break;
}
case 'backlog': {
this.backlog = newValue;
break;
}
}
}
set port(val) {
this._port = val;
this.listen();
}
get port() {
return this._port;
}
connectedCallback() {
this.prepareApp();
}
prepareApp() {
if (!this.express && !this.app) {
this.express = require('express');
this.app = this.express();
}
}
listen() {
this.prepareApp();
setTimeout(() => { //TODO this probably is not necessary. This timeout was thought to allow all properties to be set in the host element
const defaultCallback = () => {
console.log(`express app listening on port: ${this._port}`);
};
const theCallback = this._callback || defaultCallback;
this.app.listen(this._port, this.hostname, this.backlog, theCallback);
this.initChildren();
});
}
//TODO I am not sure why this method is here...if it has no useful purpose then remove it
expressReadyCallback(app, express, router, route) {
if (this.path) {
app.use(this.path, this.app);
}
else {
app.use(this.app);
}
this.initChildren();
}
initChildren() {
const _expressInitChildren = (element, app, express, router, route) => {
const children = Array.from(element.shadowRoot ? element.shadowRoot.children : element.children);
children.forEach((child) => {
if (child.expressReadyCallback) {
child.expressReadyCallback(app, express, router, route);
}
child._expressInitChildren = _expressInitChildren;
child._expressInitChildren(child, app, express, router ? router : child.router, route ? route : child.route);
});
};
_expressInitChildren(this, this.app, this.express, null, null);
}
}
window.customElements.define('express-app', ExpressApp);
</script>