-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
52 lines (42 loc) · 1.52 KB
/
app.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
'use strict';
// enables JSX requires
require('node-jsx').install({ extension: '.jsx' });
var debug = require('debug')('app');
var express = require('express');
var path = require('path');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var cachebuster = require('./cachebuster');
var serverRender = require('./server.jsx');
var app = express();
app.use(logger(app.get('env') === 'production' ? 'combined' : 'dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
// static files with cache buster
var publicPath = path.join(__dirname, 'public');
app.use(express.static(publicPath));
if (app.get('env') === 'production') {
app.get(cachebuster.path, cachebuster.remove, express.static(publicPath), cachebuster.restore);
}
if (app.get('env') === 'development') {
// run livereload and webpack dev server
require('./dev-tools');
// use webpack dev server for serving js files
app.use('/js', function (req, res) {
res.redirect('http://localhost:3001/js' + req.path);
});
}
// use react routes
app.use('/', serverRender);
// error pages
app.use(function (err, req, res, next) {
res.status(500);
// TODO: simple page for errors not in dev environment
res.send('<pre>' + err.stack + '</pre>');
});
app.set('port', process.env.PORT || 4200);
app.listen(app.get('port'), function () {
debug('Express ' + app.get('env') + ' server listening on port ' + this.address().port);
});