-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
62 lines (51 loc) · 1.16 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
'use strict';
var extend = require('xtend');
var h = require('virtual-dom/h');
var http = require('http');
var toHtml = require('vdom-to-html');
module.exports = frmwrk;
function frmwrk(render, options) {
options = extend({
port: 3000
}, options);
var handler = createHandler(render);
return createServer(handler, options.port);
}
function createServer(handler, port) {
var server = http.createServer(handler);
var listen = server.listen;
server.listen = frmwrkListen;
return server;
function frmwrkListen() {
return listen.call(server, port);
}
}
function createHandler(render) {
return frmwrkHandler;
function frmwrkHandler(req, res) {
if (req.url === '/favicon.ico') {
res.writeHead(404);
return res.end();
}
var state = {
route: {
url: req.url
}
};
var content = render(state);
var vtree = layout(content);
var html = toHtml(vtree);
res.setHeader('Content-Type', 'text/html');
res.end('<!DOCTYPE html>' + html);
}
}
function layout(content) {
return (
h('html', [
h('head', [
h('title', 'frmwrk')
]),
h('body', content)
])
);
}