-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathindex.js
78 lines (68 loc) · 2.45 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
var methods = require('methods');
module.exports = function(app, options) {
if (!options) options = {};
if (!options.helperName) options.helperName = 'url';
augmentVerbs(app);
addHelper(app, options);
addMiddleware(app, options);
};
function augmentVerbs(app) {
methods.forEach(function(method) {
var _fn = app[method];
app[method] = function(name, path) {
if ((method == 'get' && arguments.length == 1) ||
(typeof(path) != 'string' && !(path instanceof String)))
return _fn.apply(this, arguments);
if (app._namedRoutes && app._namedRoutes[name])
throw new Error('Route already defined: ' + name);
var args = Array.prototype.slice.call(arguments, 0);
args.shift();
var ret = _fn.apply(this, args);
if (!app._namedRoutes) app._namedRoutes = {};
app._namedRoutes[name] = (typeof this.route !== 'string')
? this.route(args[0]) // express 4.x
: this.routes[method].slice(-1)[0]; // express 3.x
return ret;
};
});
}
function addHelper(app, options) {
app.locals[options.helperName] = function(name, params) {
var route = app._namedRoutes[name];
if (!route) throw new Error('Route not found: ' + name);
var mountpath = (app.mountpath && app.mountpath != '/') ? app.mountpath : '';
return mountpath + reverse(app._namedRoutes[name].path, params);
};
}
function addMiddleware(app, options) {
app.use(function(req, res, next) {
res.redirectToRoute = function(status, routeName, params) {
if (isNaN(status)) {
params = routeName;
routeName = status;
}
var mountpath = (app.mountpath && app.mountpath != '/') ? app.mountpath : '';
var url = mountpath + reverse(app._namedRoutes[routeName].path, params);
if (isNaN(status)) return res.redirect(url);
else return res.redirect(status, url);
};
next();
});
}
function reverse(path, params) {
if (!params) params = {};
return path.replace(/(\/:(\w+)?(\(.+?\))?(\?)?)/g, function (m, pFull, pName, pRegex, pOptional) {
var required = !pOptional;
var param = pName;
if (required && !params.hasOwnProperty(param)) {
throw new Error('Missing value for "' + param + '".');
}
var value = params[param];
if (pRegex && value) {
if (!new RegExp('^' + pRegex + '$').test(value)) {
throw new Error('Invalid value for "' + param + '", should match "' + pRegex + '".');
}
}
return value ? '/' + value : '';
});
}