Skip to content

eggjs/egg-path-matching

Repository files navigation

egg-path-matching

NPM version CI Test coverage Known Vulnerabilities npm download

Installation

npm install egg-path-matching

Usage

import { pathMatching } from 'egg-path-matching';

const options = {
  ignore: '/api', // string will use parsed by path-to-regexp
  // support regexp
  ignore: /^\/api/,
  // support function
  ignore: ctx => ctx.path.startsWith('/api'),
  // support Array
  ignore: [ ctx => ctx.path.startsWith('/api'), /^\/foo$/, '/bar'],
  // support match or ignore
  match: '/api',
  // custom path-to-regexp module, default is `path-to-regexp@6`
  // pathToRegexpModule: customPathToRegexp,
};

const match = pathMatching(options);
assert.equal(match({ path: '/api' }), true);
assert.equal(match({ path: '/api/hello' }), true);
assert.equal(match({ path: '/api' }), true);

options

  • match {String | RegExp | Function | Array} - if request path hit options.match, will return true, otherwise will return false.
  • ignore {String | RegExp | Function | Array} - if request path hit options.ignore, will return false, otherwise will return true.
  • pathToRegexpModule {Object | Function} - custom path-to-regexp module. Default is path-to-regexp@6. Supports both path-to-regexp@6 and path-to-regexp@8+ formats.

ignore and match can not both be presented. and if neither ignore nor match presented, the new function will always return true.

License

MIT

Contributors

Contributors

Made with contributors-img.

About

url path match/ignore support string, regexp and function

Resources

License

Stars

Watchers

Forks

Contributors 7