Skip to content

Commit

Permalink
Move --spa handling to external middleware file (#150).
Browse files Browse the repository at this point in the history
  • Loading branch information
tapio committed Nov 5, 2016
1 parent 2822a99 commit 1420667
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 12 deletions.
16 changes: 5 additions & 11 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ function escape(html){
}

// Based on connect.static(), but streamlined and with added code injecter
function staticServer(root, spa) {
function staticServer(root) {
var isFile = false;
try { // For supporting mounting files instead of just directories
isFile = fs.statSync(root).isFile();
Expand All @@ -45,14 +45,6 @@ function staticServer(root, spa) {
var injectCandidates = [ new RegExp("</body>", "i"), new RegExp("</svg>") ];
var injectTag = null;

// Single Page App - redirect handler
if (spa && req.url !== '/') {
var route = req.url;
req.url = '/';
res.statusCode = 302;
res.setHeader('Location', req.url + '#' + route);
}

function directory() {
var pathname = url.parse(req.originalUrl).pathname;
res.statusCode = 301;
Expand Down Expand Up @@ -146,10 +138,9 @@ LiveServer.start = function(options) {
LiveServer.logLevel = options.logLevel === undefined ? 2 : options.logLevel;
var openPath = (options.open === undefined || options.open === true) ?
"" : ((options.open === null || options.open === false) ? null : options.open);
var spa = options.spa || false;
if (options.noBrowser) openPath = null; // Backwards compatibility with 0.7.0
var file = options.file;
var staticServerHandler = staticServer(root, spa);
var staticServerHandler = staticServer(root);
var wait = options.wait === undefined ? 100 : options.wait;
var browser = options.browser || null;
var htpasswd = options.htpasswd || null;
Expand All @@ -170,6 +161,9 @@ LiveServer.start = function(options) {
} else if (LiveServer.logLevel > 2) {
app.use(logger('dev'));
}
if (options.spa) {
middleware.push("spa");
}
// Add middleware
middleware.map(function(mw) {
if (typeof mw === "string") {
Expand Down
2 changes: 1 addition & 1 deletion live-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ for (var i = process.argv.length - 1; i >= 2; --i) {
}
}
else if (arg === "--spa") {
opts.spa = true;
opts.middleware.push("spa");
process.argv.splice(i, 1);
}
else if (arg === "--quiet" || arg === "-q") {
Expand Down
13 changes: 13 additions & 0 deletions middleware/spa.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Single Page Apps - redirect to /#/
module.exports = function(req, res, next) {
if (req.method !== "GET" && req.method !== "HEAD")
next();
if (req.url !== '/') {
var route = req.url;
req.url = '/';
res.statusCode = 302;
res.setHeader('Location', req.url + '#' + route);
res.end();
}
else next();
}

0 comments on commit 1420667

Please sign in to comment.