-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy path04-run.js
More file actions
37 lines (31 loc) · 773 Bytes
/
04-run.js
File metadata and controls
37 lines (31 loc) · 773 Bytes
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
const Rx = require('rx');
const requests_ = new Rx.Subject();
function main() {
return {
HTTP: requests_.tap(e => console.log('request to', e.req.url))
}
}
function httpEffect(model_) {
model_.subscribe(e => {
console.log('sending hello')
e.res.writeHead(200, { 'Content-Type': 'text/plain' })
e.res.end('Hello World\n')
})
}
function run(main, effects) {
const sinks = main()
Object.keys(effects).forEach(key => {
effects[key](sinks[key])
})
}
run(main, {
HTTP: httpEffect
})
const http = require('http')
const hostname = '127.0.0.1'
const port = 1337
http.createServer((req, res) => {
requests_.onNext({ req: req, res: res })
}).listen(port, hostname, () => {
console.log(`Server running at http://${hostname}:${port}/`)
});