-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.jsx
70 lines (54 loc) · 2.16 KB
/
app.jsx
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
'use strict';
import express from 'express';
import path from 'path';
import config from 'config';
import ServerRenderer from 'isomorphicTools/ServerRenderer';
console.log('Config: ', JSON.stringify(config, null, 4));
class App
{
constructor(options)
{
this.router = express.Router();
this.serverRenderer = new ServerRenderer(0);
// this site is an isomorphic (universal) reactjs web application.
// every page request is "routed" through the ServerRenderer class which takes
// the request path and produces the response.
// If you would prefer you can serve all static pages by first precompiling them
// into static files by setting 'config.serveAsPrecompiledAssets = true'. You will
// also need to add each path you want precompiled to the "PrecompiledPaths.js" file.
if (config.serveAsPrecompiledAssets)
{
this.router.use(express.static(path.join('_compiled', 'public'),{
redirect: false
}));
}
// Otherwise the application will be served directly through this server.
else
{
this.router.use('*', function(req, res, next)
{
var results = this.serverRenderer.render(req.originalUrl);
if (results.document) {
res.status(200);
res.type('html');
res.send(results.document);
} else if (results.redirect) {
res.redirect(302, results.redirect.pathname);
} else {
next();
};
}.bind(this));
this.router.use(express.static(path.join('_compiled', 'public'),{
redirect: false
}));
this.router.use('*', function(req, res, next)
{
res.status(404);
res.type('html');
res.send('page not found');
}.bind(this));
}
//
}
}
export default new App().router;