11const express = require ( 'express' ) ;
2- const history = require ( 'connect-history-api-fallback' ) ;
32const path = require ( 'path' ) ;
43const fs = require ( 'fs' ) ;
54const url = require ( 'url' ) ;
@@ -12,7 +11,6 @@ const {
1211} = require ( '../environment' ) ;
1312
1413const app = express ( ) ;
15- app . use ( history ( ) ) ;
1614
1715if ( __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 ( / \$ 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 ( ) ;
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 ( / \$ 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+
5272module . exports = app ;
0 commit comments