diff --git a/README.md b/README.md index 5dde8372..7d9267e9 100644 --- a/README.md +++ b/README.md @@ -85,6 +85,10 @@ provided locals: Display mode. `tiles` and `details` are available. Defaults to `tiles`. +##### sort + +A function to sort directorys and files, default is serveIndex.fileSortWithName,you can use serveIndex.fileSortWithMTime instead,or write one. + ## Examples ### Serve directory indexes with vanilla node.js http server diff --git a/index.js b/index.js index 7b9d856c..76043aaa 100644 --- a/index.js +++ b/index.js @@ -97,6 +97,7 @@ function serveIndex(root, options) { var stylesheet = opts.stylesheet || defaultStylesheet; var template = opts.template || defaultTemplate; var view = opts.view || 'tiles'; + var sort=opts.sort; return function (req, res, next) { if (req.method !== 'GET' && req.method !== 'HEAD') { @@ -160,7 +161,7 @@ function serveIndex(root, options) { // not acceptable if (!type) return next(createError(406)); - serveIndex[mediaType[type]](req, res, files, next, originalDir, showUp, icons, path, view, template, stylesheet); + serveIndex[mediaType[type]](req, res, files, next, originalDir, showUp, icons, path, view, template, stylesheet,sort); }); }); }; @@ -170,7 +171,7 @@ function serveIndex(root, options) { * Respond with text/html. */ -serveIndex.html = function _html(req, res, files, next, dir, showUp, icons, path, view, template, stylesheet) { +serveIndex.html = function _html(req, res, files, next, dir, showUp, icons, path, view, template, stylesheet,sort) { var render = typeof template !== 'function' ? createHtmlRender(template) : template @@ -187,9 +188,11 @@ serveIndex.html = function _html(req, res, files, next, dir, showUp, icons, path var fileList = files.map(function (file, i) { return { name: file, stat: stats[i] }; }); - + + + sort=sort||serveIndex.fileSortWithName; // sort file list - fileList.sort(fileSort); + fileList.sort(sort); // read stylesheet fs.readFile(stylesheet, 'utf8', function (err, style) { @@ -341,6 +344,22 @@ function fileSort(a, b) { String(a.name).toLocaleLowerCase().localeCompare(String(b.name).toLocaleLowerCase()); } +/** + * Sort function for with directories first.then mtime. + */ +function fileSortWithMtime(a,b){ + if (a.name === '..' || b.name === '..') { + return a.name === b.name ? 0 + : a.name === '..' ? -1 : 1; + } + var tb=b.stat?b.stat.mtime:0; + var ta=a.stat?a.stat.mtime:0; + + return Number(b.stat && b.stat.isDirectory()) - Number(a.stat && a.stat.isDirectory()) ||(tb-ta); +}; +serveIndex.fileSortWithName=fileSort; +serveIndex.fileSortWithMTime=fileSortWithMtime; + /** * Map html `dir`, returning a linked path. */ diff --git a/package.json b/package.json index a6b8da51..7838c50c 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "serve-index", "description": "Serve directory listings", - "version": "1.7.3", + "version": "1.7.4", "author": "Douglas Christopher Wilson ", "license": "MIT", "repository": "expressjs/serve-index",