@koa/router 11.0.0 → 11.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -25,6 +25,7 @@
25
25
  * Express-style routing (`app.get`, `app.put`, `app.post`, etc.)
26
26
  * Named URL parameters
27
27
  * Named routes with URL generation
28
+ * Match routes with specific host
28
29
  * Responds to `OPTIONS` requests with allowed methods
29
30
  * Support for `405 Method Not Allowed` and `501 Not Implemented`
30
31
  * Multiple route middleware
package/lib/router.js CHANGED
@@ -48,6 +48,7 @@ module.exports = Router;
48
48
  * @param {Object=} opts
49
49
  * @param {Boolean=false} opts.exclusive only run last matched route's controller when there are multiple matches
50
50
  * @param {String=} opts.prefix prefix router paths
51
+ * @param {String|RegExp=} opts.host host for router match
51
52
  * @constructor
52
53
  */
53
54
 
@@ -68,6 +69,7 @@ function Router(opts = {}) {
68
69
 
69
70
  this.params = {};
70
71
  this.stack = [];
72
+ this.host = this.opts.host;
71
73
  }
72
74
 
73
75
  /**
@@ -183,6 +185,24 @@ function Router(opts = {}) {
183
185
  * The [path-to-regexp](https://github.com/pillarjs/path-to-regexp) module is
184
186
  * used to convert paths to regular expressions.
185
187
  *
188
+ *
189
+ * ### Match host for each router instance
190
+ *
191
+ * ```javascript
192
+ * const router = new Router({
193
+ * host: 'example.domain' // only match if request host exactly equal `example.domain`
194
+ * });
195
+ *
196
+ * ```
197
+ *
198
+ * OR host cloud be a regexp
199
+ *
200
+ * ```javascript
201
+ * const router = new Router({
202
+ * host: /.*\.?example\.domain$/ // all host end with .example.domain would be matched
203
+ * });
204
+ * ```
205
+ *
186
206
  * @name get|put|post|patch|delete|del
187
207
  * @memberof module:koa-router.prototype
188
208
  * @param {String} path
@@ -358,6 +378,12 @@ Router.prototype.routes = Router.prototype.middleware = function () {
358
378
  const dispatch = function dispatch(ctx, next) {
359
379
  debug('%s %s', ctx.method, ctx.path);
360
380
 
381
+ const hostMatched = router.matchHost(ctx.host);
382
+
383
+ if (!hostMatched) {
384
+ return next();
385
+ }
386
+
361
387
  const path = router.opts.routerPath || ctx.routerPath || ctx.path;
362
388
  const matched = router.match(path, ctx.method);
363
389
  let layerChain;
@@ -735,6 +761,32 @@ Router.prototype.match = function (path, method) {
735
761
  return matched;
736
762
  };
737
763
 
764
+ /**
765
+ * Match given `input` to allowed host
766
+ * @param {String} input
767
+ * @returns {boolean}
768
+ */
769
+
770
+ Router.prototype.matchHost = function (input) {
771
+ const { host } = this;
772
+
773
+ if (!host) {
774
+ return true;
775
+ }
776
+
777
+ if (!input) {
778
+ return false;
779
+ }
780
+
781
+ if (typeof host === 'string') {
782
+ return input === host;
783
+ }
784
+
785
+ if (typeof host === 'object' && host instanceof RegExp) {
786
+ return host.test(input);
787
+ }
788
+ };
789
+
738
790
  /**
739
791
  * Run middleware for named route parameters. Useful for auto-loading or
740
792
  * validation.
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@koa/router",
3
3
  "description": "Router middleware for koa. Maintained by Forward Email and Lad.",
4
- "version": "11.0.0",
4
+ "version": "11.0.1",
5
5
  "author": "Alex Mingoia <talk@alexmingoia.com>",
6
6
  "bugs": {
7
7
  "url": "https://github.com/koajs/router/issues",