Skip to content

Commit

Permalink
feat: add function to check route matching
Browse files Browse the repository at this point in the history
  • Loading branch information
Junyi committed Aug 22, 2024
1 parent 50189c5 commit 218b734
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,17 @@ const getPath = (ctx, map) => {
return path;
};

const isMatch = (ctx, match) => {
if (typeof match === 'string') {
return ctx.path.startsWith(match);
} else if (match instanceof RegExp) {
return ctx.path.match(match);
} else if (typeof match === 'function') {
return match(ctx);
}
return false;
};

module.exports = (options = {}) => {
if (!options.host) {
throw new Error('miss option host');
Expand All @@ -40,7 +51,7 @@ module.exports = (options = {}) => {
const { protocol, host, hostname, port, pathname } = new URL(options.host);
const request = protocol === 'http:' ? http.request : https.request;
return async (ctx, next) => {
if (options.match && ctx.path.match(options.match)) {
if (isMatch(ctx, options.match)) {
const opt = {
hostname,
port: port || (protocol === 'http:' ? 80 : 443),
Expand Down

0 comments on commit 218b734

Please sign in to comment.