Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Added directory listing feature.

  • Loading branch information...
commit 42ed26aedfd84203583e07fc93d03f907296faec 1 parent 7db77d2
@airportyh authored
View
15 lib/server.js
@@ -181,8 +181,19 @@ Server.prototype = {
self.emit('file-requested', filePath)
})
} else {
- self.emit('file-requested', filePath)
- res.sendfile(filePath)
+ fs.stat(filePath, function(err, stat){
+ self.emit('file-requested', filePath)
+ if (err) return res.sendfile(filePath)
+ if (stat.isDirectory()){
+ fs.readdir(filePath, function(err, files){
+ var dirListingPage = __dirname + '/../views/directorylisting.html'
+ res.render(dirListingPage, {files: files})
+ })
+ }else{
+ res.sendfile(filePath)
+ }
+ })
+
}
}
// Everything falls back to serving a static file from the FS
View
7 tests/server_tests.js
@@ -106,6 +106,13 @@ describe('Server', function(){
})
})
+ it('lists directories', function(done){
+ request(baseUrl + 'data', function(err, req, text){
+ expect(text).to.equal('<a href="blah.txt">blah.txt</a>')
+ done()
+ })
+ })
+
describe('routes', function(){
beforeEach(function(){
config.set('routes', {
View
1  views/directorylisting.html
@@ -0,0 +1 @@
+{{#files}}<a href="{{.}}">{{.}}</a>{{/files}}
Please sign in to comment.
Something went wrong with that request. Please try again.