From 967ff69be81f380c5e9f44e52a9f79e5d78e0b9e Mon Sep 17 00:00:00 2001 From: Jack Parker Date: Tue, 15 Jul 2014 22:36:05 -0400 Subject: [PATCH 1/2] [cleanup] Whitespace cleanup --- src/index.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/index.js b/src/index.js index c70b3e0..7721c78 100644 --- a/src/index.js +++ b/src/index.js @@ -12,7 +12,7 @@ var path = require('path'); var enableMiddlewareShorthand = require('./enableMiddlewareShorthand'); module.exports = function(options) { - + var defaults = { /** @@ -46,7 +46,7 @@ module.exports = function(options) { }, // Middleware: Directory listing - // For possible options, see: + // For possible options, see: // https://github.com/expressjs/serve-index directoryListing: { enable: false, From 4fa7c7dbb6d6e94bdaf84f6bd8e41febcf56c13a Mon Sep 17 00:00:00 2001 From: Jack Parker Date: Tue, 15 Jul 2014 22:38:10 -0400 Subject: [PATCH 2/2] [FIX] Set the fallback file route on stream end If passing in multiple root directories, the fallback file gets set after each directory is served, therefore never reaching the additional directories. --- src/index.js | 34 ++++++++++++++++++++-------------- 1 file changed, 20 insertions(+), 14 deletions(-) diff --git a/src/index.js b/src/index.js index 7721c78..07135a4 100644 --- a/src/index.js +++ b/src/index.js @@ -82,23 +82,11 @@ module.exports = function(options) { } // Create server - var stream = through.obj(function(file, enc, callback) { + var files = []; + var stream = through.obj(function(file, enc, callback) { app.use(serveStatic(file.path)); - if (config.fallback) { - - var fallbackFile = file.path + '/' + config.fallback; - - if (fs.existsSync(fallbackFile)) { - - app.use(function(req, res) { - fs.createReadStream(fallbackFile).pipe(res); - }); - - } - } - if (config.livereload.enable) { watch(file.path, function(filename) { @@ -119,6 +107,24 @@ module.exports = function(options) { }); + stream.on('data', function(file) { + files.push(file) + }); + + stream.on('end', function() { + if (config.fallback) { + files.forEach(function(file){ + var fallbackFile = file.path + '/' + config.fallback; + + if (fs.existsSync(fallbackFile)) { + app.use(function(req, res) { + fs.createReadStream(fallbackFile).pipe(res); + }); + } + }); + } + }); + var webserver = http.createServer(app).listen(config.port, config.host); gutil.log('Webserver started at', gutil.colors.cyan('http://' + config.host + ':' + config.port));