-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
51 lines (35 loc) · 1.19 KB
/
main.js
File metadata and controls
51 lines (35 loc) · 1.19 KB
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
var fs = require('fs'),
path = require('path'),
express = require('express'),
bodyParser = require('body-parser'),
React = require('react'),
plates = require('plates'),
App = React.createFactory(require('./app/components/app'));
var app = express()
app.use(bodyParser.json());
app.get('/', function(req, res){
var appHtml = React.renderToString(App({ path: '/'}));
render(res, appHtml, {history: req.query.mode !== 'hash' });
})
app.use(express.static(path.join(__dirname, 'public')));
app.get('/open/:day', function(req, res) {
var appHtml = React.renderToString(App({ path: '/open/:day'}));
render(res, appHtml, {history: req.query.mode !== 'hash' });
});
app.listen(8081, function(){
console.log("Server listening on port 8081");
});
function render(res, appHtml, appData) {
fs.readFile(
path.join(__dirname, 'public', 'index.html'),
{ encoding: 'utf-8'},
function(err, tmpl) {
var html = plates.bind(tmpl, {
app: appHtml,
appData: 'APP_DATA = ' + JSON.stringify(appData)
});
res.set('Content-Type', 'text/html');
res.send(html);
}
);
}