11const fs = require ( "fs" ) ;
22const path = require ( "path" ) ;
33const http = require ( "http" ) ;
4+ const url = require ( "url" ) ;
45const httpProxy = require ( "http-proxy" ) ;
56const proxyApp = httpProxy . createProxyServer ( { autoRewrite : true } ) ;
67const proxyApi = httpProxy . createProxyServer ( { autoRewrite : true } ) ;
@@ -13,6 +14,7 @@ const serveStatic = (file, res) => {
1314 res . end ( JSON . stringify ( err ) ) ;
1415 return ;
1516 }
17+ console . log ( "serving" , file ) ;
1618 res . writeHead ( 200 ) ;
1719 res . end ( data ) ;
1820 } ) ;
@@ -67,20 +69,33 @@ var server = http.createServer(function (req, res) {
6769 } ) ;
6870
6971 proxyApp . on ( "proxyRes" , function ( proxyRes , req , res ) {
72+ console . log ( "app>>>" , req . method , target + req . url , `[${ proxyRes . statusCode } ]` ) ;
73+
74+ const uri = url . parse ( req . url ) . pathname ;
75+ // default to SWA 404 page
7076 const file404 = path . resolve ( __dirname , "404.html" ) ;
77+ const fileIndex = path . join ( process . env . SWA_EMU_APP_LOCATION , "index.html" ) ;
78+ let resource = path . join ( process . env . SWA_EMU_APP_LOCATION , req . url ) ;
79+ const isRouteRequest = ( uri ) => ( uri . split ( "/" ) . pop ( ) . indexOf ( "." ) === - 1 ? true : false ) ;
80+
7181 if ( proxyRes . statusCode === 404 ) {
7282 serveStatic ( file404 , res ) ;
7383 } else {
74- let file = path . join ( process . env . SWA_EMU_APP_LOCATION , req . url ) ;
75- if ( fs . existsSync ( file ) ) {
76- if ( fs . lstatSync ( file ) . isDirectory ( ) ) {
77- file = `${ file } index.html` ;
84+ if ( fs . existsSync ( resource ) ) {
85+ if ( fs . lstatSync ( resource ) . isDirectory ( ) ) {
86+ resource = fileIndex ;
7887 }
79- serveStatic ( file , res ) ;
8088 } else {
81- // URL/file not found on disk
82- serveStatic ( file404 , res ) ;
89+ if ( isRouteRequest ( uri ) ) {
90+ // route detected: fallback to index file
91+ resource = fileIndex ;
92+ } else {
93+ // resource not found on disk
94+ resource = file404 ;
95+ }
8396 }
97+
98+ serveStatic ( resource , res ) ;
8499 }
85100 } ) ;
86101
0 commit comments