-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
44 lines (34 loc) · 1010 Bytes
/
server.js
File metadata and controls
44 lines (34 loc) · 1010 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
38
39
40
41
42
43
44
var http = require('http');
var fs = require('fs');
var React = require('react');
var st = require('st');
// Use JSX
require('node-jsx').install({
extension: '.js',
harmony: true
});
var App = require('./components/app');
var immstruct = require('immstruct');
var mount = st({
path: __dirname + '/public',
url: '/',
cache: false,
index: 'index.html'
});
var structure = immstruct({
name: 'Doc'
});
var tpl = fs.readFileSync('./view/layout.tpl.html').toString('utf-8');
var server = http.createServer(function (req, res) {
if (req.url !== '/') return mount(req, res);
// Render React to a string, passing in our fetched tweets
var markup = React.renderToString(App({ appState: structure.cursor() }));
res.writeHead(200, { 'Content-type': 'text/html' });
res.end(
tpl.replace('{{initialApp}}', markup)
.replace('{{initialState}}', JSON.stringify(structure.current.toJSON()))
);
});
server.listen(8000, function () {
console.log('started server at port 8000');
})