-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
132 lines (121 loc) · 3.21 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
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
import Cycle from '@cycle/xstream-run'
import xs from 'xstream'
import express from 'express'
import { html, head, title, body, div, script, makeHTMLDriver } from '@cycle/dom'
import main from './driver/main'
import path from 'path'
import falcorExpress from 'falcor-express'
import falcorRouter from 'falcor-router'
function wrapVTreeWithHTMLBoilerplate(vtree) {
return (
html([
head([
title('Cycle Isomorphic')
]),
body([
div('#application', [vtree]),
script({attrs:{src: '/static/bundle.js'}})
])
])
)
}
function prependHTML5Doctype(html) {
return `<!doctype html>${html}`
}
function wrapAppResultWithBoilerplate(appFn) {
return function wrappedAppFn(sources) {
let vtree$ = appFn(sources).DOM
let wrappedVTree$ = xs.combine(wrapVTreeWithHTMLBoilerplate, vtree$)
return {
DOM: wrappedVTree$,
History: appFn(sources).History
}
}
}
const server = express()
let data = [
{
index: 'home',
data: {
title: 'The homepage[Server]',
desc: 'Welcome to our spectacular web page with nothing special here.',
link: 'Contact us'
}
},
{
index: 'about',
data: {
title: 'Read more about us[Server]',
desc: 'This is the page where we describe ourselves.',
link: 'Contact us'
}
},
{
index: 'noroute',
data: {
title: '404 Page does not exist[Server]',
desc: 'This is the landing page when route is not found.',
link: 'Contact us'
}
}
]
server.use('/model.json', falcorExpress.dataSourceRoute((req, res) => {
return new falcorRouter([
{
route: data[0].index,
get: (pathSet) => {
let _path = data[0].index
return {
path: [_path],
value: data[0].data
}
}
},
{
route: 'data.byIndex[{keys}]["title", "desc", "link"]',
get: (pathSet) => {
var res = []
pathSet[2].forEach((index) => {
data.forEach((e) => {
if(e.index == index){
pathSet[3].forEach((info) => {
res.push({
path: ['data', 'byIndex', index, info],
value: e.data[info]
})
})
}
})
})
return res
}
},
])
}))
server.use('/static', express.static(path.join(__dirname, '/static')))
server.use((req, res) => {
// Ignore favicon requests
if (req.url === '/favicon.ico') {
res.writeHead(200, {'Content-Type': 'image/x-icon'})
res.end()
return
}
console.log(`req: ${req.method} ${req.url}`)
const wrappedAppFn = wrapAppResultWithBoilerplate(main)
const context$ = xs.of({pathname: req.url});
const {sources, run} = Cycle(wrappedAppFn, {
DOM: makeHTMLDriver(),
History: () => context$,
PreventDefault: () => {}
})
const html$ = sources.DOM.elements.map(prependHTML5Doctype)
html$.addListener({
next: html => res.send(html),
error: err => res.sendStatus(500),
complete: () => {}
});
run()
})
const port = process.env.PORT || 8080
server.listen(port)
console.log(`Listening on port ${port}`)