forked from f3arnil/react-spa
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.js
61 lines (49 loc) · 1.69 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
const path = require('path');
const express = require('express');
const webpack = require('webpack');
const webpackDevMiddleware = require('webpack-dev-middleware');
const webpackHotMiddleware = require('webpack-hot-middleware');
const fs = require('fs');
const bodyParser = require('body-parser');
const config = require('./webpack.config.js');
const app = express();
const port = 3000;
const DIST_DIR = path.resolve(__dirname, 'dist');
const HTML_FILE = path.resolve(DIST_DIR, 'index.html');
const isDevelopment = process.env.NODE_ENV === 'development';
const compiler = webpack(config);
app.use(bodyParser.json());
app.put('/add-comment', (req, res) => {
const filePath = path.resolve(__dirname, 'src/resources/data.json');
let file = fs.readFileSync(filePath, 'utf8');
file = JSON.parse(file);
file.comments.push(req.body);
fs.writeFile(filePath, JSON.stringify(file), (err) => {
if (err) {
return console.log(err);
}
});
res.sendStatus(200);
});
if (isDevelopment) {
app.use(webpackDevMiddleware(compiler, {
noInfo: true,
publicPath: config.output.publicPath
}));
app.use(webpackHotMiddleware(compiler));
app.use(express.static(DIST_DIR));
app.get('*', (req, res, next) => {
compiler.outputFileSystem.readFile(HTML_FILE, (err, result) => {
if (err) {
return next(err);
}
res.set('content-type', 'text/html');
res.send(result);
res.end();
});
});
} else {
app.use(express.static(DIST_DIR));
app.get('*', (req, res) => res.sendFile(HTML_FILE));
}
app.listen(port, () => console.log(`App listening on port ${port}`));