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

Fix Windows support for mapped and built-in URLs #43

Closed
wants to merge 1 commit into from
Closed
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
51 changes: 49 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
*/

var express = require('express')
, join = require('path').join
, lingo = require('lingo')
, en = lingo.en
, orderedActions = [
Expand Down Expand Up @@ -119,7 +118,7 @@ Resource.prototype.map = function(method, path, fn){
if ('function' == typeof path) fn = path, path = '';
if ('object' == typeof path) fn = path, path = '';
if ('/' == path[0]) path = path.substr(1);
else path = join(this.param, path);
else path = posixPathJoin(this.param, path);
method = method.toLowerCase();

// setup route pathname
Expand Down Expand Up @@ -256,3 +255,51 @@ express.HTTPSServer.prototype.resource = function(name, actions, opts){
var res = this.resources[name] = new Resource(name, actions, this);
return res;
};

/**
* Use posix path module methods on Windows so that URLs do not contain \
instead of / when using functions like path.join. These posix methods are
ripped from the path module.
*/

function pathNormalizeArray(parts, allowAboveRoot) {
var up = 0;
for (var i = parts.length - 1; i >= 0; i--) {
var last = parts[i];
if (last == '.') {
parts.splice(i, 1);
} else if (last === '..') {
parts.splice(i, 1);
up++;
} else if (up) {
parts.splice(i, 1);
up--;
}
}
if (allowAboveRoot) {
for (; up--; up) {
parts.unshift('..');
}
}
return parts;
}
function posixPathNormalize(path) {
var isAbsolute = path.charAt(0) === '/',
trailingSlash = path.slice(-1) === '/';
path = pathNormalizeArray(path.split('/').filter(function(p) {
return !!p;
}), !isAbsolute).join('/');
if (!path && !isAbsolute) {
path = '.';
}
if (path && trailingSlash) {
path += '/';
}
return (isAbsolute ? '/' : '') + path;
};
function posixPathJoin() {
var paths = Array.prototype.slice.call(arguments, 0);
return posixPathNormalize(paths.filter(function(p, index) {
return p && typeof p === 'string';
}).join('/'));
};