Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add sort option #54

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
27 changes: 23 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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') {
Expand Down Expand Up @@ -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);
});
});
};
Expand All @@ -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
Expand All @@ -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) {
Expand Down Expand Up @@ -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.
*/
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "serve-index",
"description": "Serve directory listings",
"version": "1.7.3",
"version": "1.7.4",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please undo this version bump.

"author": "Douglas Christopher Wilson <[email protected]>",
"license": "MIT",
"repository": "expressjs/serve-index",
Expand Down