express-set-domain
Version:
Force an express app to use a particular domain
24 lines (18 loc) • 544 B
JavaScript
;
/**
* Intercepts requests ad 301 redirects to correct domain
* @returns {function} express middleware
*/
module.exports = function (domain) {
if (!domain || domain === '') throw new Error('Invalid domain');
return function (req, res, next) {
var protocol = req.protocol;
if (domain === req.host) {
next();
}
else {
// domain does not match, redirect using same protocol
res.redirect(301, protocol + '://' + domain + req.url);
}
};
};