1
1
const express = require ( 'express' ) ;
2
- const history = require ( 'connect-history-api-fallback' ) ;
3
2
const path = require ( 'path' ) ;
4
3
const fs = require ( 'fs' ) ;
5
4
const url = require ( 'url' ) ;
@@ -12,7 +11,6 @@ const {
12
11
} = require ( '../environment' ) ;
13
12
14
13
const app = express ( ) ;
15
- app . use ( history ( ) ) ;
16
14
17
15
if ( __DEV__ ) {
18
16
const webpack = require ( 'webpack' ) ;
@@ -26,27 +24,49 @@ if (__DEV__) {
26
24
cached : false ,
27
25
colors : true ,
28
26
} ,
27
+ serverSideRender : true ,
28
+ index : false ,
29
29
} ) ) ;
30
30
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
+ } ) ;
31
41
} 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 ) => {
42
44
const filePath = path . resolve ( frontendBuildPath , 'index.html' ) ;
43
45
fs . readFile ( filePath , 'utf8' , ( err , data ) => {
44
- if ( err ) next ( err ) ;
45
- const result = data . replace ( / \$ T I T L E / g , title ) . replace ( / \$ D E S C R I P T I O N / g , description ) ;
46
- res . send ( result ) ;
46
+ if ( err ) return next ( err ) ;
47
+ res . indexFile = data ;
48
+ next ( ) ;
47
49
} ) ;
48
50
} ) ;
49
- app . use ( express . static ( frontendBuildPath ) ) ;
50
51
}
51
52
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 ( / \$ T I T L E / g, title )
67
+ . replace ( / \$ D E S C R I P T I O N / g, description )
68
+ . replace ( / \$ A L G O R I T H M / g, algorithm ? JSON . stringify ( algorithm ) . replace ( / < / g, '\\u003c' ) : 'undefined' ) ;
69
+ res . send ( indexFile ) ;
70
+ } ) ;
71
+
52
72
module . exports = app ;
0 commit comments