Permalink
Browse files

st handler now supports multiple rootPaths

  • Loading branch information...
dimsmol committed Apr 2, 2013
1 parent e9da283 commit 410c318a78eb949d1fc9a34889bdc604d1efb3a9
Showing with 19 additions and 6 deletions.
  1. +19 −6 lib/handlers/static.js
View
@@ -6,7 +6,7 @@ var errors = require('../errors');
var HttpOnly = require('./http_only');
-var Static = function (rootPath, opt_options) {
+var Static = function (rootPaths, opt_options) {
HttpOnly.call(this, opt_options && opt_options.errorHandler);
this.options = ops.cloneWithDefaults(opt_options, {
single: false,
@@ -18,23 +18,36 @@ var Static = function (rootPath, opt_options) {
index: 'index.html'
});
- this.rootPath = rootPath;
+ if (!Array.isArray(rootPaths)) {
+ rootPaths = [rootPaths];
+ }
+ this.rootPaths = rootPaths;
};
inherits(Static, HttpOnly);
Static.prototype.name = 'Static';
Static.prototype.handleHttpRequest = function (ctx) {
var path = this.extractPath(ctx);
+ this.send(ctx, path);
+};
+
+Static.prototype.send = function (ctx, path, idx) {
+ idx = idx || 0;
var options = this.options;
var self = this;
var sendStream = send(ctx.req, path)
- .root(this.rootPath)
+ .root(this.rootPaths[idx])
.hidden(options.hidden)
.maxage(options.maxAge)
.index(options.allowIndex && options.index)
.on('error', function (err) {
- self.onError(ctx, sendStream, err);
+ if (err.status == 404 && (idx + 1) < self.rootPaths.length) {
+ self.send(ctx, path, idx + 1);
+ }
+ else {
+ self.onError(ctx, sendStream, err);
+ }
})
.on('directory', function () {
self.onDir(ctx, sendStream);
@@ -92,8 +105,8 @@ Static.prototype.onEnd = function (ctx, sendStream) {
ctx.next();
};
-Static.st = function (rootPath, opt_options) {
- return new Static(rootPath, opt_options);
+Static.st = function (rootPaths, opt_options) {
+ return new Static(rootPaths, opt_options);
};

0 comments on commit 410c318

Please sign in to comment.