diff --git a/lib/middleware/directory.js b/lib/middleware/directory.js index ac8ea7a5c..b8ed241fd 100644 --- a/lib/middleware/directory.js +++ b/lib/middleware/directory.js @@ -1,4 +1,3 @@ - /*! * Connect - directory * Copyright(c) 2011 Sencha Inc. @@ -56,6 +55,7 @@ var mediaType = { * - `hidden` display hidden (dot) files. Defaults to false. * - `icons` display icons. Defaults to false. * - `filter` Apply this filter function to files. Defaults to false. + * - `template` Optional path to html template. Defaults to __dirname + '/../public/directory.html' * * @param {String} root * @param {Object} options @@ -72,7 +72,8 @@ exports = module.exports = function directory(root, options){ , icons = options.icons , view = options.view || 'tiles' , filter = options.filter - , root = normalize(root + sep); + , root = normalize(root + sep) + , template = options.template || __dirname + '/../public/directory.html'; return function directory(req, res, next) { if ('GET' != req.method && 'HEAD' != req.method) return next(); @@ -110,7 +111,7 @@ exports = module.exports = function directory(root, options){ // not acceptable if (!type) return next(utils.error(406)); - exports[mediaType[type]](req, res, files, next, originalDir, showUp, icons, path, view); + exports[mediaType[type]](req, res, files, next, originalDir, showUp, icons, path, view, template); }); }); }; @@ -120,8 +121,8 @@ exports = module.exports = function directory(root, options){ * Respond with text/html. */ -exports.html = function(req, res, files, next, dir, showUp, icons, path, view){ - fs.readFile(__dirname + '/../public/directory.html', 'utf8', function(err, str){ +exports.html = function(req, res, files, next, dir, showUp, icons, path, view, template){ + fs.readFile(template, 'utf8', function(err, str){ if (err) return next(err); fs.readFile(__dirname + '/../public/style.css', 'utf8', function(err, style){ if (err) return next(err); diff --git a/test/directory.js b/test/directory.js index a121f35b1..17a6a35c3 100644 --- a/test/directory.js +++ b/test/directory.js @@ -1,4 +1,3 @@ - var connect = require('..'); var app = connect(); @@ -118,6 +117,26 @@ describe('directory()', function(){ }); }); + describe('when setting a custom template', function () { + var app = connect(connect.directory('test/fixtures',{template: __dirname + '/shared/template.html'})); + + it('should respond with file list and testing template sentence', function (done) { + app.request() + .get('/') + .set('Accept', 'text/html') + .end(function(res){ + res.statusCode.should.equal(200); + res.should.be.html; + res.body.should.include('users'); + res.body.should.include('g# %3 o %2525 %37 dir'); + res.body.should.include('file #1.txt'); + res.body.should.include('todo.txt'); + res.body.should.include('This is the test template'); + done(); + }); + }); + }); + describe('when set with trailing slash', function () { var app = connect(connect.directory('test/fixtures/')); diff --git a/test/shared/template.html b/test/shared/template.html new file mode 100644 index 000000000..a59eaf462 --- /dev/null +++ b/test/shared/template.html @@ -0,0 +1,83 @@ + + + + + + listing directory {directory} + + + + + +

This is the test template

+
+

{linked-path}

+ {files} +
+ +