-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathserver.js
86 lines (66 loc) · 1.87 KB
/
server.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
const express = require('express')
const app = express()
const html = '<!doctype HTML><html><head><meta charset="utf-8"><title>Server side!</title></head><body><div id="app">{HTML}</div></body></html>'
class StringElement {
constructor(tag, children = []) {
this.tag = tag
this.children = children
}
appendChild(child) {
this.children.push(child)
}
attrsToString() {
let stringAttrs = ''
Object.keys(this).forEach(attr => {
if (attr === 'tag' || attr === 'children') {
return
}
stringAttrs += `${attr}="${this[attr]}"`
})
return stringAttrs
}
toString() {
return `<${this.tag} ${this.attrsToString()}>${this.children.map(child => child.toString()).join('')}</${this.tag}>`
}
}
const serverDocument = {
createElement(tag) {
return new StringElement(tag)
},
createTextNode(node) {
return node
}
}
function createDOM(document) {
return ['div', 'span', 'label', 'p', 'h1', 'input', 'button', 'form'].reduce(function(DOM, tag) {
DOM[tag] = function(attrs, children) {
var elem = document.createElement(tag)
elem = Object.assign(elem, attrs)
if (typeof children === 'string') {
elem.appendChild(document.createTextNode(children))
} else {
children = children || []
children.forEach(function(child) {
var child = typeof child === 'string' ? document.createTextNode(child) : child
elem.appendChild(child)
})
return elem
}
}
return DOM
}, {})
}
var DOM = createDOM(serverDocument)
function renderDOM(DOM, elem, fel) {
elem.innerHTML = ''
elem.appendChild(DOM)
}
function renderApp(state) {
return DOM.span({}, [state.text])
}
app.get('*', (req, res) => {
res.send(html.replace('{HTML}', renderApp({ text: 'Jiný text' })))
})
app.listen(9999, () => {
console.log('App is running on port:', 9999)
})