forked from carlosnat/kataReact
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
65 lines (53 loc) · 1.63 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
53
54
55
56
57
58
59
60
61
62
63
64
65
import React from 'react';
import ReactDOMServer from 'react-dom/server';
import {StaticRouter} from 'react-router';
import routes from './src/routes';
import path from 'path';
import morgan from 'morgan';
import express from 'express';
import bodyParser from 'body-parser';
import webpack from 'webpack';
import WebpackDevServer from 'webpack-dev-server';
require('dotenv').config();
const isDev = process.env.NODE_ENV === 'development';
const port = process.env.PORT;
const title = process.env.TITLE;
const app = express();
if (!port) {
console.error('please rename config file and then edit the file. (.envcpy to .env)');
process.exit(0);
}
let config = require('./webpack.config.dev');
let compiler = webpack(config);
let devPort = process.env.DEV_PORT;
let devServer = new WebpackDevServer(compiler, config.devServer);
devServer.listen(devPort, () => {
console.log('webpack-dev-server is listening on port', devPort);
});
app.use(morgan('dev'));
app.use(bodyParser.urlencoded({extended: false}));
app.use(bodyParser.json());
app.use('/assets', express.static(path.join(__dirname, 'assets')));
app.get('*', (req, res) => {
let context = {};
let html = ReactDOMServer.renderToString(
<StaticRouter location={req.url} context={context}>
{routes}
</StaticRouter>
);
if (context.url) {
res.redirect(301, context.url);
} else {
res.render(path.resolve(__dirname, 'index.pug'), {
TITLE: title,
CONTENT: html,
DEVELOPMENT: isDev
});
}
});
app.use((err, req, res) => {
res.status(500).send('500 Error');
});
app.listen(port, () => {
console.log('express server listening on port ', devPort);
});