Connect
exports.html()
)) {
exportskey;
return;
}
}
// not acceptable
next(utils.error(406));
});
});
};
};
/**
Respond with text/html.
Source
exports.html = function(req, res, files, next, dir, showUp, icons){
fs.readFile(__dirname + '/../public/directory.html', 'utf8', function(err, str){
if (err) return next(err);
fs.readFile(__dirname + '/../public/style.css', 'utf8', function(err, style){
if (err) return next(err);
if (showUp) files.unshift('..');
str = str
.replace('{style}', style)
.replace('{files}', html(files, dir, icons))
.replace('{directory}', dir)
.replace('{linked-path}', htmlPath(dir));
res.setHeader('Content-Type', 'text/html');
res.setHeader('Content-Length', str.length);
res.end(str);
});
});
};exports.json()
Respond with application/json.
Source
exports.json = function(req, res, files){
files = JSON.stringify(files);
res.setHeader('Content-Type', 'application/json');
res.setHeader('Content-Length', files.length);
res.end(files);
};exports.plain()
Respond with text/plain.
Source
exports.plain = function(req, res, files){
files = files.join('\n') + '\n';
res.setHeader('Content-Type', 'text/plain');
res.setHeader('Content-Length', files.length);
res.end(files);
};htmlPath()
Map html dir, returning a linked path.
Source
function htmlPath(dir) {
var curr = [];
return dir.split('/').map(function(part){
curr.push(part);
return '' + part + '';
}).join(' / ');
}html()
Map html files, returning an html unordered list.
Source
function html(files, dir, useIcons) {
return '' + files.map(function(file){
var icon = ''
, classes = [];
if (useIcons && '..' != file) {
icon = icons[extname(file)] || icons.default;
icon = '
';
classes.push('icon');
}
return '- '
+ icon + file + '
';
}).join('\n') + '
';
}load()
Load and cache the given icon.
Source
function load(icon) {
if (cache[icon]) return cache[icon];
return cache[icon] = fs.readFileSync(__dirname + '/../public/icons/' + icon, 'base64');
}removeHidden()
Filter "hidden" files, aka files
beginning with a ..
Source
function removeHidden(files) {
return files.filter(function(file){
return '.' != file[0];
});
}icons
Icon map.
Source
var icons = {
'.js': 'page_white_code_red.png'
, '.c': 'page_white_c.png'
, '.h': 'page_white_h.png'
, '.cc': 'page_white_cplusplus.png'
, '.php': 'page_white_php.png'
, '.rb': 'page_white_ruby.png'
, '.cpp': 'page_white_cplusplus.png'
, '.swf': 'page_white_flash.png'
, '.pdf': 'page_white_acrobat.png'
, 'default': 'page_white.png'
};
Directory
Serve directory listings with the given
rootpath.Options
hiddendisplay hidden (dot) files. Defaults to false.iconsdisplay icons. Defaults to false.filterApply this filter function to files. Defaults to false.Source