-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
35 lines (28 loc) · 821 Bytes
/
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
(function() {
/**
* Returns a plugin that will redirect by passing rules, e.g. from www.example.com => example.com.
*
* @return {Function} restify handler.
*/
function wwwCanonical(options) {
options = options || {};
options = extend(defaultOptions, options);
return function canonicalization(request, response, next) {
if (request.headers.host.match(options.from)) {
response.writeHead(options.code, {
Location: options.protocol + '://' + (options.to || request.headers.host.replace(options.from, ''))
});
response.end('');
} else {
return next();
}
}
}
var extend = require('util')._extend;
const defaultOptions = {
from: 'www.',
code: 301,
protocol: 'http'
};
module.exports = wwwCanonical;
})();