-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathrewrite.js
29 lines (22 loc) · 898 Bytes
/
rewrite.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
var escapeRegExp = require('../utils/escapeRegExp');
var isRegExp = require('../utils/isRegExp');
/**
* A middleware that provides URL rewriting behavior similar to Apache's
* mod_rewrite. The pathname of requests that match the given pattern is
* overwritten with the replacement using a simple String#replace.
*/
function rewrite(app, pattern, replacement) {
if (typeof pattern === 'string')
pattern = new RegExp('^' + escapeRegExp(pattern) + '$');
if (!isRegExp(pattern))
throw new Error('Rewrite pattern must be a RegExp or String');
replacement = replacement || '';
return function (conn) {
var pathname = conn.pathname;
// Modify the pathname if the pattern matches.
if (pattern.test(pathname))
conn.location.properties.pathname = conn.basename + pathname.replace(pattern, replacement);
return conn.call(app);
};
}
module.exports = rewrite;