diff --git a/.gitignore b/.gitignore index 073cfe2..d95a66e 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,3 @@ npm-debug.log node_modules - +.idea diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..c89b579 --- /dev/null +++ b/.travis.yml @@ -0,0 +1,4 @@ +language: node_js +node_js: + - "0.12" + - "stable" \ No newline at end of file diff --git a/README.md b/README.md index 70eec00..6829269 100644 --- a/README.md +++ b/README.md @@ -194,6 +194,15 @@ example: `{ indexFile: "index.htm" }` > Defaults to `index.html` +#### `defaultExtension` # + +Choose a default extension when serving files. +A request to '/myFile' would check for a `myFile` folder (first) then a `myFile.html` (second). + +example: `{ defaultExtension: "html" }` + +> Defaults to `null` + Command Line Interface ---------------------- diff --git a/lib/node-static.js b/lib/node-static.js index 6ba88ed..01e0711 100644 --- a/lib/node-static.js +++ b/lib/node-static.js @@ -37,6 +37,12 @@ var Server = function (root, options) { this.serverInfo = 'node-static/' + version.join('.'); } + if ('defaultExtension' in this.options) { + this.defaultExtension = '.'+this.options.defaultExtension; + } else { + this.defaultExtension = null; + } + this.defaultHeaders['server'] = this.serverInfo; if (this.cache !== false) { @@ -141,7 +147,21 @@ Server.prototype.servePath = function (pathname, status, headers, req, res, fini if (pathname.indexOf(that.root) === 0) { fs.stat(pathname, function (e, stat) { if (e) { - finish(404, {}); + // possibly not found, check default extension + if (that.defaultExtension) { + fs.stat(pathname+that.defaultExtension, function(e2, stat2) { + if (e2) { + // really not found + finish(404, {}); + } else if (stat2.isFile()) { + that.respond(null, status, headers, [pathname+that.defaultExtension], stat2, req, res, finish); + } else { + finish(400, {}); + } + }); + } else { + finish(404, {}); + } } else if (stat.isFile()) { // Stream a single file. that.respond(null, status, headers, [pathname], stat, req, res, finish); } else if (stat.isDirectory()) { // Stream a directory of files. @@ -204,7 +224,7 @@ Server.prototype.gzipOk = function(req, contentType) { } /* Send a gzipped version of the file if the options and the client indicate gzip is enabled and - * we find a .gz file mathing the static resource requested. + * we find a .gz file matching the static resource requested. */ Server.prototype.respondGzip = function(pathname, status, contentType, _headers, files, stat, req, res, finish) { var that = this; diff --git a/test/fixtures/there.html b/test/fixtures/there.html new file mode 100644 index 0000000..bb2a3c4 --- /dev/null +++ b/test/fixtures/there.html @@ -0,0 +1,8 @@ + +
+