Compare commits
17 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
9fbc4bf182 | ||
|
|
027f6dcf54 | ||
|
|
f7eb0ae92a | ||
|
|
51e9763c0a | ||
|
|
dd6a4a864a | ||
|
|
cff5890bce | ||
|
|
6d9e23c5ac | ||
|
|
36d68485ab | ||
|
|
e40300aa86 | ||
|
|
5771318d78 | ||
|
|
d006e3a123 | ||
|
|
a2364a8abf | ||
|
|
90e39a9a4f | ||
|
|
480fdcc468 | ||
|
|
b4cd08b1a3 | ||
|
|
c1b2ad4a71 | ||
|
|
e18a68fc94 |
@@ -2,3 +2,4 @@ language: node_js
|
||||
node_js:
|
||||
- "0.8"
|
||||
- "0.10"
|
||||
- "0.11"
|
||||
|
||||
20
History.md
Normal file
20
History.md
Normal file
@@ -0,0 +1,20 @@
|
||||
1.0.3 / 2014-05-20
|
||||
==================
|
||||
|
||||
* Fix error from non-statable files in HTML view
|
||||
|
||||
1.0.2 / 2014-04-28
|
||||
==================
|
||||
|
||||
* Add `stylesheet` option
|
||||
* deps: negotiator@0.4.3
|
||||
|
||||
1.0.1 / 2014-03-05
|
||||
==================
|
||||
|
||||
* deps: negotiator@0.4.2
|
||||
|
||||
1.0.0 / 2014-03-05
|
||||
==================
|
||||
|
||||
* Genesis from connect
|
||||
37
Readme.md
37
Readme.md
@@ -1,19 +1,37 @@
|
||||
# Serve Index
|
||||
# serve-index [](https://travis-ci.org/expressjs/serve-index) [](http://badge.fury.io/js/serve-index)
|
||||
|
||||
Previously `connect.directory()`.
|
||||
Serves pages that contain directory listings for a given path.
|
||||
|
||||
Usage:
|
||||
## API
|
||||
|
||||
```js
|
||||
var connect = require('connect');
|
||||
var serveIndex = require('serve-index');
|
||||
var express = require('express')
|
||||
var directory = require('serve-index')
|
||||
|
||||
var app = connect();
|
||||
var app = express()
|
||||
|
||||
app.use(serveIndex('public/ftp', {'icons': true}));
|
||||
app.listen();
|
||||
app.use(directory('public/ftp', {'icons': true}))
|
||||
app.listen()
|
||||
```
|
||||
|
||||
### directory(path, options)
|
||||
|
||||
Returns middlware that serves an index of the directory in the given `path`.
|
||||
|
||||
#### Options
|
||||
|
||||
- `hidden` - display hidden (dot) files. Defaults to `false`.
|
||||
- `view` - display mode. `tiles` and `details` are available. Defaults to `tiles`.
|
||||
- `icons` - display icons. Defaults to `false`.
|
||||
- `filter` - Apply this filter function to files. Defaults to `false`.
|
||||
- `stylesheet` - Optional path to a CSS stylesheet. Defaults to a built-in stylesheet.
|
||||
- `template` - Optional path to an HTML template. Defaults to a built-in template.
|
||||
- The following tokens are replaced in templates:
|
||||
- `{directory}` with the name of the directory.
|
||||
- `{files}` with the HTML of an unordered list of file links.
|
||||
- `{linked-path}` with the HTML of a link to the directory.
|
||||
- `{style}` with the specified stylesheet and embedded images.
|
||||
|
||||
## License
|
||||
|
||||
The MIT License (MIT)
|
||||
@@ -37,3 +55,6 @@ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
|
||||
The [Silk](http://www.famfamfam.com/lab/icons/silk/) icons are created
|
||||
by/copyright of [FAMFAMFAM](http://www.famfamfam.com/).
|
||||
|
||||
47
index.js
47
index.js
@@ -1,6 +1,6 @@
|
||||
|
||||
/*!
|
||||
* Connect - directory
|
||||
* Expressjs | Connect - directory
|
||||
* Copyright(c) 2011 Sencha Inc.
|
||||
* Copyright(c) 2011 TJ Holowaychuk
|
||||
* Copyright(c) 2014 Douglas Christopher Wilson
|
||||
@@ -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.
|
||||
@@ -60,25 +60,13 @@ var mediaType = {
|
||||
};
|
||||
|
||||
/**
|
||||
* Directory:
|
||||
*
|
||||
* Serve directory listings with the given `root` path.
|
||||
*
|
||||
* Options:
|
||||
* See Readme.md for documentation of options.
|
||||
*
|
||||
* - `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 a built-in template.
|
||||
* The following tokens are replaced:
|
||||
* - `{directory}` with the name of the directory.
|
||||
* - `{files}` with the HTML of an unordered list of file links.
|
||||
* - `{linked-path}` with the HTML of a link to the directory.
|
||||
* - `{style}` with the built-in CSS and embedded images.
|
||||
*
|
||||
* @param {String} root
|
||||
* @param {String} path
|
||||
* @param {Object} options
|
||||
* @return {Function}
|
||||
* @return {Function} middleware
|
||||
* @api public
|
||||
*/
|
||||
|
||||
@@ -92,7 +80,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();
|
||||
@@ -130,7 +119,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);
|
||||
});
|
||||
});
|
||||
};
|
||||
@@ -140,7 +129,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){
|
||||
@@ -292,9 +281,12 @@ function html(files, dir, useIcons, view) {
|
||||
|
||||
path.push(encodeURIComponent(file.name));
|
||||
|
||||
var date = file.name == '..' ? ''
|
||||
: file.stat.mtime.toDateString()+' '+file.stat.mtime.toLocaleTimeString();
|
||||
var size = isDir ? '' : file.stat.size;
|
||||
var date = file.stat && file.name !== '..'
|
||||
? file.stat.mtime.toDateString() + ' ' + file.stat.mtime.toLocaleTimeString()
|
||||
: '';
|
||||
var size = file.stat && !isDir
|
||||
? file.stat.size
|
||||
: '';
|
||||
|
||||
return '<li><a href="'
|
||||
+ normalizeSlashes(normalize(path.join('/')))
|
||||
@@ -362,7 +354,14 @@ function stat(dir, files, cb) {
|
||||
|
||||
files.forEach(function(file){
|
||||
batch.push(function(done){
|
||||
fs.stat(join(dir, file), done);
|
||||
fs.stat(join(dir, file), function(err, stat){
|
||||
if (err && err.code !== 'ENOENT') {
|
||||
// pass ENOENT as null stat, not error
|
||||
return done(err);
|
||||
}
|
||||
|
||||
done(null, stat || null);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
10
package.json
10
package.json
@@ -1,7 +1,7 @@
|
||||
{
|
||||
"name": "serve-index",
|
||||
"description": "Serve directory listings",
|
||||
"version": "1.0.0",
|
||||
"version": "1.0.3",
|
||||
"author": "Douglas Christopher Wilson <doug@somethingdoug.com>",
|
||||
"license": "MIT",
|
||||
"repository": {
|
||||
@@ -13,12 +13,12 @@
|
||||
},
|
||||
"dependencies": {
|
||||
"batch": "0.5.0",
|
||||
"negotiator": "0.3.0"
|
||||
"negotiator": "0.4.3"
|
||||
},
|
||||
"devDependencies": {
|
||||
"connect": "^2.13.0",
|
||||
"mocha": "^1.17.0",
|
||||
"should": "^3.0.0",
|
||||
"connect": "~2.14.1",
|
||||
"mocha": "~1.17.1",
|
||||
"should": "~3.1.3",
|
||||
"supertest": "~0.9.0"
|
||||
},
|
||||
"engines": {
|
||||
|
||||
3
test/shared/styles.css
Normal file
3
test/shared/styles.css
Normal file
@@ -0,0 +1,3 @@
|
||||
body {
|
||||
color: #00ff00;
|
||||
}
|
||||
20
test/test.js
20
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 () {
|
||||
|
||||
Reference in New Issue
Block a user