@koa/router 11.0.0 → 11.0.2
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 +1 -0
- package/lib/router.js +53 -3
- package/package.json +2 -1
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
|
@@ -5,15 +5,13 @@
|
|
|
5
5
|
* @link https://github.com/alexmingoia/koa-router
|
|
6
6
|
*/
|
|
7
7
|
|
|
8
|
-
const { debuglog } = require('util');
|
|
9
|
-
|
|
10
8
|
const compose = require('koa-compose');
|
|
11
9
|
const HttpError = require('http-errors');
|
|
12
10
|
const methods = require('methods');
|
|
13
11
|
const { pathToRegexp } = require('path-to-regexp');
|
|
14
12
|
const Layer = require('./layer');
|
|
15
13
|
|
|
16
|
-
const debug =
|
|
14
|
+
const debug = require('debug')('koa-router');
|
|
17
15
|
|
|
18
16
|
/**
|
|
19
17
|
* @module koa-router
|
|
@@ -48,6 +46,7 @@ module.exports = Router;
|
|
|
48
46
|
* @param {Object=} opts
|
|
49
47
|
* @param {Boolean=false} opts.exclusive only run last matched route's controller when there are multiple matches
|
|
50
48
|
* @param {String=} opts.prefix prefix router paths
|
|
49
|
+
* @param {String|RegExp=} opts.host host for router match
|
|
51
50
|
* @constructor
|
|
52
51
|
*/
|
|
53
52
|
|
|
@@ -68,6 +67,7 @@ function Router(opts = {}) {
|
|
|
68
67
|
|
|
69
68
|
this.params = {};
|
|
70
69
|
this.stack = [];
|
|
70
|
+
this.host = this.opts.host;
|
|
71
71
|
}
|
|
72
72
|
|
|
73
73
|
/**
|
|
@@ -183,6 +183,24 @@ function Router(opts = {}) {
|
|
|
183
183
|
* The [path-to-regexp](https://github.com/pillarjs/path-to-regexp) module is
|
|
184
184
|
* used to convert paths to regular expressions.
|
|
185
185
|
*
|
|
186
|
+
*
|
|
187
|
+
* ### Match host for each router instance
|
|
188
|
+
*
|
|
189
|
+
* ```javascript
|
|
190
|
+
* const router = new Router({
|
|
191
|
+
* host: 'example.domain' // only match if request host exactly equal `example.domain`
|
|
192
|
+
* });
|
|
193
|
+
*
|
|
194
|
+
* ```
|
|
195
|
+
*
|
|
196
|
+
* OR host cloud be a regexp
|
|
197
|
+
*
|
|
198
|
+
* ```javascript
|
|
199
|
+
* const router = new Router({
|
|
200
|
+
* host: /.*\.?example\.domain$/ // all host end with .example.domain would be matched
|
|
201
|
+
* });
|
|
202
|
+
* ```
|
|
203
|
+
*
|
|
186
204
|
* @name get|put|post|patch|delete|del
|
|
187
205
|
* @memberof module:koa-router.prototype
|
|
188
206
|
* @param {String} path
|
|
@@ -358,6 +376,12 @@ Router.prototype.routes = Router.prototype.middleware = function () {
|
|
|
358
376
|
const dispatch = function dispatch(ctx, next) {
|
|
359
377
|
debug('%s %s', ctx.method, ctx.path);
|
|
360
378
|
|
|
379
|
+
const hostMatched = router.matchHost(ctx.host);
|
|
380
|
+
|
|
381
|
+
if (!hostMatched) {
|
|
382
|
+
return next();
|
|
383
|
+
}
|
|
384
|
+
|
|
361
385
|
const path = router.opts.routerPath || ctx.routerPath || ctx.path;
|
|
362
386
|
const matched = router.match(path, ctx.method);
|
|
363
387
|
let layerChain;
|
|
@@ -735,6 +759,32 @@ Router.prototype.match = function (path, method) {
|
|
|
735
759
|
return matched;
|
|
736
760
|
};
|
|
737
761
|
|
|
762
|
+
/**
|
|
763
|
+
* Match given `input` to allowed host
|
|
764
|
+
* @param {String} input
|
|
765
|
+
* @returns {boolean}
|
|
766
|
+
*/
|
|
767
|
+
|
|
768
|
+
Router.prototype.matchHost = function (input) {
|
|
769
|
+
const { host } = this;
|
|
770
|
+
|
|
771
|
+
if (!host) {
|
|
772
|
+
return true;
|
|
773
|
+
}
|
|
774
|
+
|
|
775
|
+
if (!input) {
|
|
776
|
+
return false;
|
|
777
|
+
}
|
|
778
|
+
|
|
779
|
+
if (typeof host === 'string') {
|
|
780
|
+
return input === host;
|
|
781
|
+
}
|
|
782
|
+
|
|
783
|
+
if (typeof host === 'object' && host instanceof RegExp) {
|
|
784
|
+
return host.test(input);
|
|
785
|
+
}
|
|
786
|
+
};
|
|
787
|
+
|
|
738
788
|
/**
|
|
739
789
|
* Run middleware for named route parameters. Useful for auto-loading or
|
|
740
790
|
* 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.
|
|
4
|
+
"version": "11.0.2",
|
|
5
5
|
"author": "Alex Mingoia <talk@alexmingoia.com>",
|
|
6
6
|
"bugs": {
|
|
7
7
|
"url": "https://github.com/koajs/router/issues",
|
|
@@ -12,6 +12,7 @@
|
|
|
12
12
|
"@koajs"
|
|
13
13
|
],
|
|
14
14
|
"dependencies": {
|
|
15
|
+
"debug": "^4.3.4",
|
|
15
16
|
"http-errors": "^2.0.0",
|
|
16
17
|
"koa-compose": "^4.1.0",
|
|
17
18
|
"methods": "^1.1.2",
|