Skip to content

Commit 0e96a29

Browse files
committed
Pass algorithm data to client along with index.html
1 parent 2fd5a69 commit 0e96a29

File tree

7 files changed

+368
-295
lines changed

7 files changed

+368
-295
lines changed

app/backend.js

Lines changed: 11 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,15 @@
1-
const proxy = require('http-proxy-middleware');
21
const {
32
__DEV__,
4-
proxyPort,
53
backendBuildPath,
6-
apiEndpoint,
74
} = require('../environment');
85

96
if (__DEV__) {
107
const webpack = require('webpack');
11-
128
const webpackConfig = require('../webpack.backend.config.js');
13-
149
const compiler = webpack(webpackConfig);
1510

11+
let backend = null;
1612
let lastHash = null;
17-
let httpServer = null;
1813
compiler.watch({
1914
watchOptions: {
2015
ignored: /public/,
@@ -24,30 +19,24 @@ if (__DEV__) {
2419
lastHash = null;
2520
compiler.purgeInputFileSystem();
2621
console.error(err);
27-
}
28-
if (stats.hash !== lastHash) {
22+
} else if (stats.hash !== lastHash) {
2923
lastHash = stats.hash;
3024
console.info(stats.toString({
3125
cached: false,
3226
colors: true,
3327
}));
3428

35-
try {
36-
if (httpServer) httpServer.close();
37-
delete require.cache[require.resolve(backendBuildPath)];
38-
const app = require(backendBuildPath).default;
39-
httpServer = app.listen(proxyPort);
40-
} catch (e) {
41-
console.error(e);
42-
}
29+
delete require.cache[require.resolve(backendBuildPath)];
30+
backend = require(backendBuildPath).default;
4331
}
4432
});
4533

46-
module.exports = proxy({
47-
target: `http://localhost:${proxyPort}/`,
48-
pathRewrite: { ['^' + apiEndpoint]: '' },
49-
ws: true,
50-
});
34+
const backendWrapper = (req, res, next) => backend(req, res, next);
35+
backendWrapper.getHierarchy = () => backend.hierarchy;
36+
module.exports = backendWrapper;
5137
} else {
52-
module.exports = require(backendBuildPath).default;
38+
const backend = require(backendBuildPath).default;
39+
const backendWrapper = (req, res, next) => backend(req, res, next);
40+
backendWrapper.getHierarchy = () => backend.hierarchy;
41+
module.exports = backendWrapper;
5342
}

app/frontend.js

Lines changed: 36 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
const express = require('express');
2-
const history = require('connect-history-api-fallback');
32
const path = require('path');
43
const fs = require('fs');
54
const url = require('url');
@@ -12,7 +11,6 @@ const {
1211
} = require('../environment');
1312

1413
const app = express();
15-
app.use(history());
1614

1715
if (__DEV__) {
1816
const webpack = require('webpack');
@@ -26,27 +24,49 @@ if (__DEV__) {
2624
cached: false,
2725
colors: true,
2826
},
27+
serverSideRender: true,
28+
index: false,
2929
}));
3030
app.use(webpackHot(compiler));
31+
app.use((req, res, next) => {
32+
const { fs } = res.locals;
33+
const outputPath = res.locals.webpackStats.toJson().outputPath;
34+
const filePath = path.resolve(outputPath, 'index.html');
35+
fs.readFile(filePath, 'utf8', (err, data) => {
36+
if (err) return next(err);
37+
res.indexFile = data;
38+
next();
39+
});
40+
});
3141
} else {
32-
const { hierarchy } = require('./backend'); // TODO: Hmm...
33-
app.get('/index.html', (req, res, next) => {
34-
const [, categoryKey, algorithmKey] = url.parse(req.originalUrl).pathname.split('/');
35-
let { title, description } = packageJson;
36-
const algorithm = hierarchy.find(categoryKey, algorithmKey);
37-
if (algorithm) {
38-
title = [algorithm.categoryName, algorithm.algorithmName].join(' - ');
39-
description = algorithm.description;
40-
}
41-
42+
app.use(express.static(frontendBuildPath));
43+
app.use((req, res, next) => {
4244
const filePath = path.resolve(frontendBuildPath, 'index.html');
4345
fs.readFile(filePath, 'utf8', (err, data) => {
44-
if (err) next(err);
45-
const result = data.replace(/\$TITLE/g, title).replace(/\$DESCRIPTION/g, description);
46-
res.send(result);
46+
if (err) return next(err);
47+
res.indexFile = data;
48+
next();
4749
});
4850
});
49-
app.use(express.static(frontendBuildPath));
5051
}
5152

53+
app.use((req, res) => {
54+
const backend = require('./backend');
55+
const hierarchy = backend.getHierarchy();
56+
57+
const [, categoryKey, algorithmKey] = url.parse(req.originalUrl).pathname.split('/');
58+
let { title, description } = packageJson;
59+
const algorithm = hierarchy.find(categoryKey, algorithmKey);
60+
if (algorithm) {
61+
title = [algorithm.categoryName, algorithm.algorithmName].join(' - ');
62+
description = algorithm.description;
63+
}
64+
65+
const indexFile = res.indexFile
66+
.replace(/\$TITLE/g, title)
67+
.replace(/\$DESCRIPTION/g, description)
68+
.replace(/\$ALGORITHM/g, algorithm ? JSON.stringify(algorithm).replace(/</g, '\\u003c') : 'undefined');
69+
res.send(indexFile);
70+
});
71+
5272
module.exports = app;

environment.js

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ const {
66

77
HTTP_PORT = '8080',
88
HTTPS_PORT = '8443',
9-
PROXY_PORT = '3000',
109

1110
GITHUB_CLIENT_ID,
1211
GITHUB_CLIENT_SECRET,
@@ -26,7 +25,6 @@ const __DEV__ = !__PROD__;
2625

2726
const httpPort = parseInt(HTTP_PORT);
2827
const httpsPort = parseInt(HTTPS_PORT);
29-
const proxyPort = parseInt(PROXY_PORT);
3028

3129
const githubClientId = GITHUB_CLIENT_ID;
3230
const githubClientSecret = GITHUB_CLIENT_SECRET;
@@ -54,7 +52,6 @@ module.exports = {
5452
__DEV__,
5553
httpPort,
5654
httpsPort,
57-
proxyPort,
5855
githubClientId,
5956
githubClientSecret,
6057
githubWebhookSecret,

0 commit comments

Comments
 (0)