Skip to content

Commit

Permalink
Merge pull request #167 from alex996/enhancement/fallback-option
Browse files Browse the repository at this point in the history
Add optional --fallback CLI option
  • Loading branch information
alallier committed Dec 2, 2018
2 parents ad6d18c + c15f18e commit 045314e
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
3 changes: 2 additions & 1 deletion bin/reload
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ program.version(require('../package.json').version)
.option('-e, --exts [extensions]', 'Extensions separated by commas or pipes. Defaults to html,js,css.', 'html|js|css')
.option('-p, --port [port]', 'The port to bind to. Can be set with PORT env variable as well. Defaults to 8080', '8080')
.option('-s, --start-page [start-page]', 'Specify a start page. Defaults to index.html', 'index.html')
.option('-f, --fallback [fallback]', 'Fallback to the start page when route is not found')
.option('-v, --verbose [verbose]', 'Turning on logging on the server and client side. Defaults to false', false)
.parse(process.argv)

Expand All @@ -29,7 +30,7 @@ if (typeof program.watchDir === 'undefined') {
program.watchDir = program.dir
}

var args = ['-e', program.exts, '-w', program.watchDir, '-q', '--', serverFile, program.port, program.dir, !!program.browser, program.hostname, runFile, program.startPage, program.verbose]
var args = ['-e', program.exts, '-w', program.watchDir, '-q', '--', serverFile, program.port, program.dir, !!program.browser, program.hostname, runFile, program.startPage, program.fallback, program.verbose]
supervisor.run(args)

console.log('\nReload web server:')
Expand Down
12 changes: 10 additions & 2 deletions lib/reload-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ var openBrowser = (argv._[2] === 'true')
var hostname = argv._[3]
var runFile = argv._[4]
var startPage = argv._[5]
var verbose = (argv._[6] === 'true')
var fallback = (argv._[6] === 'true')
var verbose = (argv._[7] === 'true')

var reloadOpts = {
port: port,
Expand All @@ -33,10 +34,17 @@ var server = http.createServer(function (req, res) {
var pathname = url.pathname.replace(/(\/)(.*)/, '$2') // Strip leading `/` so we can find files on file system

var fileEnding = pathname.split('.')[1]
var noFileEnding = fileEnding === undefined

if (fileEnding === 'html' || pathname === '/' || pathname === '') { // Server side inject reload code to html files
if (fileEnding === 'html' || pathname === '/' || pathname === '' || noFileEnding) { // Server side inject reload code to html files
if (pathname === '/' || pathname === '') {
pathname = dir + '/' + startPage
} else if (noFileEnding) {
if (fs.existsSync(dir + '/' + pathname + '.html')) {
pathname = dir + '/' + pathname + '.html'
} else if (fallback) {
pathname = dir + '/' + startPage
}
} else {
pathname = dir + '/' + pathname
}
Expand Down

0 comments on commit 045314e

Please sign in to comment.