koa-router
Advanced tools
Comparing version 11.0.0 to 11.0.1
@@ -51,2 +51,3 @@ /** | ||
* @param {String=} opts.prefix prefix router paths | ||
* @param {String|RegExp=} opts.host host for router match | ||
* @constructor | ||
@@ -72,2 +73,3 @@ */ | ||
this.stack = []; | ||
this.host = this.opts.host; | ||
} | ||
@@ -188,2 +190,20 @@ | ||
* | ||
* | ||
* ### Match host for each router instance | ||
* | ||
* ```javascript | ||
* const router = new Router({ | ||
* host: 'example.domain' // only match if request host exactly equal `example.domain` | ||
* }); | ||
* | ||
* ``` | ||
* | ||
* OR host cloud be a regexp | ||
* | ||
* ```javascript | ||
* const router = new Router({ | ||
* host: /.*\.?example\.domain$/ // all host end with .example.domain would be matched | ||
* }); | ||
* ``` | ||
* | ||
* @name get|put|post|patch|delete|del | ||
@@ -364,2 +384,8 @@ * @memberof module:koa-router.prototype | ||
const hostMatched = router.matchHost(ctx.host); | ||
if (!hostMatched) { | ||
return next(); | ||
} | ||
const path = router.opts.routerPath || ctx.routerPath || ctx.path; | ||
@@ -743,2 +769,28 @@ const matched = router.match(path, ctx.method); | ||
/** | ||
* Match given `input` to allowed host | ||
* @param {String} input | ||
* @returns {boolean} | ||
*/ | ||
Router.prototype.matchHost = function (input) { | ||
const { host } = this; | ||
if (!host) { | ||
return true; | ||
} | ||
if (!input) { | ||
return false; | ||
} | ||
if (typeof host === 'string') { | ||
return input === host; | ||
} | ||
if (typeof host === 'object' && host instanceof RegExp) { | ||
return host.test(input); | ||
} | ||
}; | ||
/** | ||
* Run middleware for named route parameters. Useful for auto-loading or | ||
@@ -745,0 +797,0 @@ * validation. |
{ | ||
"name": "koa-router", | ||
"description": "Router middleware for koa. Maintained by Forward Email and Lad.", | ||
"version": "11.0.0", | ||
"version": "11.0.1", | ||
"author": "Alex Mingoia <talk@alexmingoia.com>", | ||
@@ -6,0 +6,0 @@ "bugs": { |
@@ -28,2 +28,3 @@ # [@koa/router](https://github.com/koajs/router) | ||
* Named routes with URL generation | ||
* Match routes with specific host | ||
* Responds to `OPTIONS` requests with allowed methods | ||
@@ -30,0 +31,0 @@ * Support for `405 Method Not Allowed` and `501 Not Implemented` |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
32159
958
86
1