-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
executable file
·82 lines (68 loc) · 1.92 KB
/
server.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
var express = require('express');
var app = express();
/************************************************************
*
* Express routes for:
* - app.js
* - style.css
* - index.html
*
* Sample endpoints to demo async data fetching:
* - POST /landing
* - POST /home
*
************************************************************/
// Serve application file depending on environment
app.get('/dist/app.js', function (req, res) {
if (process.env.PRODUCTION) {
res.sendFile(__dirname + '/dist/app.js');
} else {
res.redirect('//localhost:9090/dist/app.js');
}
});
// Serve aggregate stylesheet depending on environment
app.get('/dist/style.css', function (req, res) {
if (process.env.PRODUCTION) {
res.sendFile(__dirname + '/dist/style.css');
} else {
res.redirect('//localhost:9090/dist/style.css');
}
});
app.use(express.static(__dirname + '/dist'));
// Serve index page
app.get('*', function (req, res) {
res.sendFile(__dirname + '/index.html');
});
/*************************************************************
*
* Webpack Dev Server
*
* See: http://webpack.github.io/docs/webpack-dev-server.html
*
*************************************************************/
if (!process.env.PRODUCTION) {
var webpack = require('webpack');
var WebpackDevServer = require('webpack-dev-server');
var config = require('./webpack.local.config');
new WebpackDevServer(webpack(config), {
publicPath: config.output.publicPath,
hot: true,
noInfo: true,
historyApiFallback: true
}).listen(9090, 'localhost', function (err, result) {
if (err) {
console.log(err);
}
});
}
/******************
*
* Express server
*
*****************/
var port = process.env.PORT || 8080;
var server = app.listen(port, function () {
var host = server.address().address;
var port = server.address().port;
console.log('Essential React listening at http://%s:%s', host, port);
});