Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
npm-debug.log
node_modules

.idea
4 changes: 4 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
language: node_js
node_js:
- "0.12"
- "stable"
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
----------------------
Expand Down
24 changes: 22 additions & 2 deletions lib/node-static.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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;
Expand Down
8 changes: 8 additions & 0 deletions test/fixtures/there.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<html lang="en">
<head>
<title>Awesome page</title>
</head>
<body>
hello there!
</body>
</html>
56 changes: 55 additions & 1 deletion test/integration/node-static-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -386,5 +386,59 @@ suite.addBatch({
assert.equal(body, 'hello world');
}
}
}).export(module);
}).addBatch({
'finding a file by default extension': {
topic: function() {
server.close();

fileServer = new static.Server(__dirname+'/../fixtures', {defaultExtension: "txt"});

server = require('http').createServer(function(request, response) {
fileServer.serve(request, response);
}).listen(TEST_PORT);
request.get(TEST_SERVER + '/hello', this.callback);
},
'should respond with 200': function(error, response, body) {
assert.equal(response.statusCode, 200);
},
'should respond with text/plain': function(error, response, body) {
assert.equal(response.headers['content-type'], 'text/plain');
},
'should respond with hello world': function(error, response, body) {
assert.equal(body, 'hello world');
}
}
}).addBatch({
'default extension does not interfere with folders': {
topic : function(){
server.close();

fileServer = new static.Server(__dirname+'/../fixtures', {defaultExtension: "html"});

server = require('http').createServer(function(request, response) {
fileServer.serve(request, response);
}).listen(TEST_PORT);
request.get({ url: TEST_SERVER + '/there', followRedirect: false }, this.callback); // without trailing slash
},
'should respond with 301' : function(error, response, body){
assert.equal(response.statusCode, 301);
},
'should respond with location header': function(error, response, body){
assert.equal(response.headers['location'], '/there/'); // now with trailing slash
},
'should respond with empty string body' : function(error, response, body){
assert.equal(body, '');
}
}
}).addBatch({
'terminate server': {
topic: function() {
server.close(this.callback);
},
'should be listening': function() {
/* This test is necessary to ensure the topic execution.
* A topic without tests will be not executed */
assert.isTrue(true);
}
}
}).export(module);