-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
64 lines (52 loc) · 1.53 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
63
64
process.chdir(__dirname);
var http = require('http');
var express = require('express');
var exphbs = require('express-handlebars');
function reactHandler(request, response) {
if (module.parent || process.env.NODE_ENV === 'production') {
response.render('production', { layout: 'react' });
} else {
response.render('development', { layout: 'react' });
}
}
function selected(option, value) {
if (option === value) {
return ' selected';
}
return '';
}
var app;
try {
console.info('Starting server.');
// Create the app and set up the default stack.
app = express();
// Set up handlebars
app.engine('.hbs', exphbs({
extname: '.hbs',
defaultLayout: 'main',
helpers: {
selected: selected
}
}));
app.set('views', __dirname + '/views');
app.set('view engine', '.hbs');
// Static consents
app.use('/static', express.static('static'));
// Use handlebars to render react contents
app.get('/', reactHandler);
if (module.parent) {
module.exports = http.createServer(app);
console.info('Server is ready.');
} else {
// Static routes for development mode
app.use('/web', express.static('web'));
app.use('/jspm_packages', express.static('jspm_packages'));
app.use('/jspm.browser.js', express.static('jspm.browser.js'));
app.use('/jspm.config.js', express.static('jspm.config.js'));
app.listen(8080, function () {
console.info('Dashboard listening on port 8080!');
});
}
} catch (error) {
console.error('Failed to start the server: ' + error.stack);
}