From e40300aa86a5fd58df226ee3edcaadcaebd1aaa2 Mon Sep 17 00:00:00 2001 From: Chris O'Connor Date: Mon, 28 Apr 2014 14:10:35 -0400 Subject: [PATCH] Add stylesheet option closes #2 --- index.js | 9 +++++---- test/shared/styles.css | 3 +++ test/test.js | 20 ++++++++++++++++++++ 3 files changed, 28 insertions(+), 4 deletions(-) create mode 100644 test/shared/styles.css diff --git a/index.js b/index.js index 74699e4..50783f2 100644 --- a/index.js +++ b/index.js @@ -41,7 +41,7 @@ var defaultTemplate = join(__dirname, 'public', 'directory.html'); * Stylesheet. */ -var stylesheet = join(__dirname, 'public', 'style.css'); +var defaultStylesheet = join(__dirname, 'public', 'style.css'); /** * Media types and the map for content negotiation. @@ -93,7 +93,8 @@ exports = module.exports = function directory(root, options){ , view = options.view || 'tiles' , filter = options.filter , root = normalize(root + sep) - , template = options.template || defaultTemplate; + , template = options.template || defaultTemplate + , stylesheet = options.stylesheet || defaultStylesheet; return function directory(req, res, next) { if ('GET' != req.method && 'HEAD' != req.method) return next(); @@ -131,7 +132,7 @@ exports = module.exports = function directory(root, options){ // not acceptable if (!type) return next(createError(406)); - exports[mediaType[type]](req, res, files, next, originalDir, showUp, icons, path, view, template); + exports[mediaType[type]](req, res, files, next, originalDir, showUp, icons, path, view, template, stylesheet); }); }); }; @@ -141,7 +142,7 @@ exports = module.exports = function directory(root, options){ * Respond with text/html. */ -exports.html = function(req, res, files, next, dir, showUp, icons, path, view, template){ +exports.html = function(req, res, files, next, dir, showUp, icons, path, view, template, stylesheet){ fs.readFile(template, 'utf8', function(err, str){ if (err) return next(err); fs.readFile(stylesheet, 'utf8', function(err, style){ diff --git a/test/shared/styles.css b/test/shared/styles.css new file mode 100644 index 0000000..3310f40 --- /dev/null +++ b/test/shared/styles.css @@ -0,0 +1,3 @@ +body { + color: #00ff00; +} \ No newline at end of file diff --git a/test/test.js b/test/test.js index 81fbbc6..5b822de 100644 --- a/test/test.js +++ b/test/test.js @@ -161,6 +161,26 @@ describe('directory()', function(){ }); }); + describe('when setting a custom stylesheet', function () { + var server; + before(function () { + server = createServer('test/fixtures', {'stylesheet': __dirname + '/shared/styles.css'}); + }); + after(function (done) { + server.close(done); + }); + + it('should respond with appropriate embedded styles', function (done) { + request(server) + .get('/') + .set('Accept', 'text/html') + .expect(200) + .expect('Content-Type', /html/) + .expect(/color: #00ff00;/) + .end(done); + }); + }); + describe('when set with trailing slash', function () { var server; before(function () {