Skip to content

Commit 7497f95

Browse files
committed
Updated server.js to render static assets in prod env
1 parent d4d3f22 commit 7497f95

File tree

1 file changed

+14
-12
lines changed

1 file changed

+14
-12
lines changed

docs/server.js

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,27 +5,29 @@ const config = require('./webpack.config.development');
55
const logger = require('express-logger');
66

77
const app = express();
8-
const compiler = webpack(config);
98

10-
app.use(require('webpack-dev-middleware')(compiler, {
11-
noInfo: true,
12-
publicPath: config.output.publicPath,
13-
stats: {
14-
colors: true
15-
}
16-
}));
17-
18-
app.use(logger({ path: `./log/${process.env.NODE_ENV}.log` }));
19-
20-
// Run webpack middleware only in dev environment
219
if (process.env.NODE_ENV !== 'production') {
10+
// Run webpack middleware only in dev environment
11+
const compiler = webpack(config);
12+
app.use(require('webpack-dev-middleware')(compiler, {
13+
noInfo: true,
14+
publicPath: config.output.publicPath,
15+
stats: {
16+
colors: true
17+
}
18+
}));
2219
app.use(require('webpack-hot-middleware')(compiler));
20+
} else {
21+
// Use build folder for serving static assets in prod environment
22+
app.use(express.static('build'));
2323
}
2424

2525
app.get('*', function (req, res) {
2626
res.sendFile(path.join(__dirname, './public/index.html'));
2727
});
2828

29+
app.use(logger({ path: `./log/${process.env.NODE_ENV}.log` }));
30+
2931
app.listen(8001, '0.0.0.0', function (err) {
3032
if (err) {
3133
console.log(err);

0 commit comments

Comments
 (0)