Created
October 31, 2018 15:05
-
-
Save ChukwuEmekaAjah/6c8316e641e336303b04a209c501523d to your computer and use it in GitHub Desktop.
vhost main function
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Create a vhost middleware. | |
* | |
* @param {string|RegExp} hostname | |
* @param {function} handle | |
* @return {Function} | |
* @public | |
*/ | |
function vhost(hostname, handle) { | |
if (!hostname) { | |
throw new TypeError('argument hostname is required') | |
} | |
if (!handle) { | |
throw new TypeError('argument handle is required') | |
} | |
if (typeof handle !== 'function') { | |
throw new TypeError('argument handle must be a function') | |
} | |
// create regular expression for hostname | |
var regexp = hostregexp(hostname) | |
return function vhost(req, res, next) { | |
var vhostdata = vhostof(req, regexp) | |
if (!vhostdata) { | |
return next() | |
} | |
// populate | |
req.vhost = vhostdata | |
// handle | |
handle(req, res, next) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment