File tree Expand file tree Collapse file tree 2 files changed +16
-4
lines changed Expand file tree Collapse file tree 2 files changed +16
-4
lines changed Original file line number Diff line number Diff line change 1
1
# file-domain-server
2
2
3
- ** file-domain-server** is a simplistic Nodejs HTTP file server that serve
3
+ ** file-domain-server** is a simplistic Nodejs HTTP file server that serves
4
4
different folders depending on the HTTP ` Host ` header. It is mainly intended to
5
- run an internal network with its own DNS server.
5
+ run on an internal network with its own DNS server.
6
6
7
7
## Get started
8
8
@@ -52,6 +52,10 @@ The following can be configured as environment variables:
52
52
53
53
- ** DEFAULT_INDEX_FILE** name of file served on paths ending with a trailing
54
54
slash (default ` index.html ` ).
55
+ - ** FALLBACK_FILE** name of file served as a fallback if requested file is not
56
+ found. Set to ` index.html ` to enable client-side routing or to e.g.
57
+ ` error.html ` to display an error page (note that response still returns a 200
58
+ status code). It is undefined by default and no fallback file will be served.
55
59
- ** PORT** the port the server should listen on (default 8080).
56
60
- ** ROOT_FILE_PATH** root directory of served file tree (default ` build ` ).
57
61
Original file line number Diff line number Diff line change @@ -4,6 +4,7 @@ const mime = require("mime");
4
4
const path = require ( "path" ) ;
5
5
6
6
const DEFAULT_INDEX_FILE = process . env . DEFAULT_INDEX_FILE || "index.html" ;
7
+ const FALLBACK_FILE = process . env . FALLBACK_FILE || null ;
7
8
const PORT = process . env . PORT || 8080 ;
8
9
const ROOT_FILE_PATH = process . env . ROOT_FILE_PATH || "build" ;
9
10
@@ -45,7 +46,14 @@ function handleRequest(req, res) {
45
46
console . debug ( "Requesting" , domain , filePath ) ;
46
47
47
48
const actualFilePath = createFilePath ( domain , filePath ) ;
48
- const file = getFile ( actualFilePath ) ;
49
+ let file = getFile ( actualFilePath ) ;
50
+
51
+ if ( ! file && FALLBACK_FILE ) {
52
+ const fallbackFilePath = createFilePath ( domain , FALLBACK_FILE ) ;
53
+ console . debug ( "Serving fallback file:" , fallbackFilePath ) ;
54
+ file = getFile ( fallbackFilePath ) ;
55
+ }
56
+
49
57
if ( ! file ) {
50
58
res . writeHead ( 404 ) ;
51
59
res . end ( ) ;
@@ -62,4 +70,4 @@ function handleRequest(req, res) {
62
70
const server = http . createServer ( handleRequest ) ;
63
71
server . listen ( PORT ) ;
64
72
65
- console . info ( `Listening on port ${ PORT } ` )
73
+ console . info ( `Listening on port ${ PORT } ` ) ;
You can’t perform that action at this time.
0 commit comments