Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Allow staticDirHandler to take a prefix if the req.url differs from t…

…he actual location within the root directory
  • Loading branch information...
commit 7808b7a3d5a12fde1c5a3016ff184845bf3d06b5 1 parent 313b036
Devin Torres authored
Showing with 6 additions and 5 deletions.
  1. +6 −5 lib/node-router.js
View
11 lib/node-router.js
@@ -287,7 +287,7 @@ exports.staticHandler = function (filename) {
};
};
-exports.staticDirHandler = function(root) {
+exports.staticDirHandler = function(root, prefix) {
function loadResponseData(req, res, filename, callback) {
var content_type = mime.getMime(filename);
var encoding = (content_type.slice(0,4) === "text" ? "utf8" : "binary");
@@ -306,11 +306,12 @@ exports.staticDirHandler = function(root) {
}
return function (req, res) {
- // trim off any query/anchor stuff, and lose the leading slash
- var filename = req.url.replace(/\?.*$/g, '').replace(/#$/g, '').substring(1);
+ // trim off any query/anchor stuff
+ var filename = req.url.replace(/[\?|#].*$/, '');
+ if (prefix) filename = filename.replace(new RegExp('^'+prefix), '');
// make sure nobody can explore our local filesystem
- filename = root + filename.replace(/\.\./g, '.');
- if(filename == root) filename = root + 'index.html';
+ filename = path.join(root, filename.replace(/\.\./g, '.'));
+ if (filename == root) filename = path.join(root + 'index.html');
loadResponseData(req, res, filename, function(headers, body, encoding) {
res.writeHead(200, headers);
res.write(body, encoding);
Please sign in to comment.
Something went wrong with that request. Please try again.