@@ -2,7 +2,6 @@ const fs = require("fs");
22const path = require ( "path" ) ;
33const http = require ( "http" ) ;
44const httpProxy = require ( "http-proxy" ) ;
5- const { dirname } = require ( "path" ) ;
65const proxyApp = httpProxy . createProxyServer ( { autoRewrite : true } ) ;
76const proxyApi = httpProxy . createProxyServer ( { autoRewrite : true } ) ;
87const proxyAuth = httpProxy . createProxyServer ( { autoRewrite : false } ) ;
@@ -20,6 +19,7 @@ const serveStatic = (file, res) => {
2019} ;
2120
2221var server = http . createServer ( function ( req , res ) {
22+ // proxy AUTH request to AUTH emulator
2323 if ( req . url . startsWith ( "/.auth" ) ) {
2424 const target = process . env . SWA_EMU_AUTH_URI || "http://localhost:4242" ;
2525 req . url = `/app${ req . url } ` ;
@@ -37,7 +37,10 @@ var server = http.createServer(function (req, res) {
3737 console . log ( err . message ) ;
3838 proxyAuth . close ( ) ;
3939 } ) ;
40- } else if ( req . url . startsWith ( `/${ process . env . SWA_EMU_API_PREFIX || "api" } ` ) ) {
40+ }
41+
42+ // proxy API request to local API
43+ else if ( req . url . startsWith ( `/${ process . env . SWA_EMU_API_PREFIX || "api" } ` ) ) {
4144 const target = process . env . SWA_EMU_API_URI || "http://localhost:7071" ;
4245 console . log ( "api>" , req . method , target + req . url ) ;
4346
@@ -49,7 +52,10 @@ var server = http.createServer(function (req, res) {
4952 console . log ( err . message ) ;
5053 proxyApi . close ( ) ;
5154 } ) ;
52- } else {
55+ }
56+
57+ // proxy APP request to local APP
58+ else {
5359 const target = process . env . SWA_EMU_APP_URI || "http://localhost:4200" ;
5460 console . log ( "app>" , req . method , target + req . url ) ;
5561
@@ -61,15 +67,20 @@ var server = http.createServer(function (req, res) {
6167 } ) ;
6268
6369 proxyApp . on ( "proxyRes" , function ( proxyRes , req , res ) {
70+ const file404 = path . resolve ( __dirname , "404.html" ) ;
6471 if ( proxyRes . statusCode === 404 ) {
65- const file = path . resolve ( __dirname , "404.html" ) ;
66- serveStatic ( file , res ) ;
72+ serveStatic ( file404 , res ) ;
6773 } else {
6874 let file = path . join ( process . env . SWA_EMU_APP_LOCATION , req . url ) ;
69- if ( fs . lstatSync ( file ) . isDirectory ( ) ) {
70- file = `${ file } index.html` ;
75+ if ( fs . existsSync ( file ) ) {
76+ if ( fs . lstatSync ( file ) . isDirectory ( ) ) {
77+ file = `${ file } index.html` ;
78+ }
79+ serveStatic ( file , res ) ;
80+ } else {
81+ // URL/file not found on disk
82+ serveStatic ( file404 , res ) ;
7183 }
72- serveStatic ( file , res ) ;
7384 }
7485 } ) ;
7586
@@ -79,7 +90,7 @@ var server = http.createServer(function (req, res) {
7990 "Content-Type" : "text/plain" ,
8091 } ) ;
8192
82- res . end ( `Something went wrong.\n ${ err . toString ( ) } ` ) ;
93+ res . end ( err . toString ( ) ) ;
8394 } ) ;
8495 }
8596} ) ;
0 commit comments