Skip to content

Instantly share code, notes, and snippets.

@ChukwuEmekaAjah
Created October 31, 2018 15:05
Show Gist options
  • Save ChukwuEmekaAjah/6c8316e641e336303b04a209c501523d to your computer and use it in GitHub Desktop.
Save ChukwuEmekaAjah/6c8316e641e336303b04a209c501523d to your computer and use it in GitHub Desktop.
vhost main function
/**
* 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