Skip to content

Commit

Permalink
showdir stuff, untested
Browse files Browse the repository at this point in the history
  • Loading branch information
James Halliday committed Nov 21, 2011
1 parent b388fc5 commit 59ed68a
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 8 deletions.
45 changes: 45 additions & 0 deletions lib/showdir.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
var fs = require('fs');
var ent = require('ent');

module.exports = function (dir, res) {
fs.readdir(dir, function (err, xs) {
if (err) {
res.statusCode = 500;
res.end(err && err.stack || err.toString());
}
else {
var pending = xs.length;
var dirs = [], files = [], errs = [];

function finish () {
dirs.sort().forEach(function (file) {
res.write(
'<a href="' + encodeURI(file) + '">'
+ ent.encode(file) + '/'
+ '</a>'
);
});

files.sort().forEach(function (file) {
res.write(
'<a href="' + encodeURI(file) + '">'
+ ent.encode(file)
+ '</a>'
);
});

res.end();
}

xs.forEach(function (file) {
fs.stat(dir + '/' + file, function (err, s) {
if (err) errs.push(err)
else if (s.isDirectory()) dirs.push(file)
else files.push(file)

if (--pending === 0) finish()
});
});
}
});
};
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
},
"main": "./ecstatic.js",
"scripts": {
"test": "node example.js"
"test": "tap test/*.js"
},
"keywords" : [
"static",
Expand All @@ -23,7 +23,8 @@
"node": "*"
},
"dependencies": {
"mime" : "1.2.x"
"mime" : "1.2.x",
"ent" : "0.0.x"
},
"devDependencies": {
"tap" : "0.0.x",
Expand Down
12 changes: 6 additions & 6 deletions test/express.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ var files = {
body : 'B!!!\n',
},
'c.js' : {
type : 'text/javascript',
type : 'application/javascript',
body : 'console.log(\'C!!!\');\n',
},
'd.js' : {
type : 'text/javascript',
type : 'application/javascript',
body : 'console.log(\'C!!!\');\n',
},
'subdir/e.html' : {
Expand All @@ -39,7 +39,7 @@ var files = {

test('express', function (t) {
var filenames = Object.keys(files);
t.plan(filenames.length);
t.plan(filenames.length * 2);
var port = Math.floor(Math.random() * ((1<<16) - 1e4) + 1e4);

var app = express.createServer();
Expand All @@ -51,10 +51,10 @@ test('express', function (t) {
request.get(uri, function (err, res, body) {
if (err) t.fail(err);
t.equal(
files[file].type, res.headers['content-type'],
'content-type differs for ' + file
res.headers['content-type'], files[file].type,
'content-type for ' + file
);
t.equal(files[file].body, body, 'body differs for ' + file);
t.equal(body, files[file].body, 'body for ' + file);

if (--pending === 0) {
app.close();
Expand Down

0 comments on commit 59ed68a

Please sign in to comment.