Giter Club home page Giter Club logo

path-match's Introduction

Koa Path Match

NPM version Node.js CI Test coverage License Downloads

A simple routing wrapper around path-match. Similar to koa-route, except it optionally handles methods better. All of these routers use path-to-regexp underneath, which is what Express uses as well.

const route = require('koa-path-match')({/* options passed to path-to-regexp */})

app.use(route('/:id(\\d+)', (ctx, next) => {
  const id = ctx.params.id

  // do stuff
  switch (ctx.request.method) {

  }
}))

Or you can create middleware per method:

app.use(route('/:id(\\d+)')
  .get(async ctx => {
    ctx.body = await Things.getById(ctx.params.id)
  })
  .delete(async ctx => {
    await Things.delete(ctx.params.id)
    ctx.status = 204
  })
)

Maintainer

API

route(path, fns...)

paths are just like Express routes. fns is either a single middleware or nested arrays of middleware, just like Express.

const router = route(path)

When you don't set fns in the route() function, a router instance is returned.

router[method](fns...)

Define a middleware just for a specific method.

app.use(route('/:id(\\d+)').get(async ctx => {
  ctx.body = await Things.getById(ctx.params.id)
}))
  • next is not passed as a parameter. I consider this an anti-pattern in Koa - one route/method, one function.

this.params

Any keys defined in the path will be set to ctx.params, overwriting any already existing keys defined.

path-match's People

Contributors

3imed-jaberi avatar dependabot[bot] avatar greenkeeper[bot] avatar greenkeeperio-bot avatar haoxins avatar ifraixedes avatar jonathanong avatar snyk-bot avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

path-match's Issues

An in-range update of eslint-plugin-standard is breaking the build 🚨

Version 2.2.0 of eslint-plugin-standard just got published.

Branch Build failing 🚨
Dependency eslint-plugin-standard
Current Version 2.1.1
Type devDependency

This version is covered by your current version range and after updating it in your project the build failed.

As eslint-plugin-standard is β€œonly” a devDependency of this project it might not break production or downstream projects, but β€œonly” your build or test tools – preventing new deploys or publishes.

I recommend you give this issue a high priority. I’m sure you can resolve this πŸ’ͺ


Status Details
  • ❌ continuous-integration/travis-ci/push The Travis CI build failed Details
Commits

The new version differs by 3 commits .

See the full diff.

Not sure how things should work exactly?

There is a collection of frequently asked questions and of course you may always ask my humans.


Your Greenkeeper Bot 🌴

An in-range update of supertest is breaking the build 🚨

Version 3.3.0 of supertest was just published.

Branch Build failing 🚨
Dependency supertest
Current Version 3.2.0
Type devDependency

This version is covered by your current version range and after updating it in your project the build failed.

supertest is a devDependency of this project. It might not break your production code or affect downstream projects, but probably breaks your build or test tools, which may prevent deploying or publishing.

Status Details
  • ❌ continuous-integration/travis-ci/push: The Travis CI build could not complete due to an error (Details).

Release Notes v3.3.0

#509 - Fix #486, bug in _assertBody, switch to deepStrictEqual (thanks @mikelax)
#510 - Refactor test files to use const/let (thanks @rimiti)

Commits

The new version differs by 10 commits.

  • e910e85 chore: Prepare for v3.3.0 release.
  • bd864de Merge pull request #511 from visionmedia/bugfix-486-equal
  • 101fbf5 Merge branch 'master' into bugfix-486-equal
  • 04230bb Merge pull request #510 from visionmedia/refact-const-let
  • 510a7ae bugfix: 486 Change method to use deepStrictEqual. (#509)
  • 913150d chore(.editorconfig) [*.md] block removed
  • 82e0828 refact(test/supertest.js) vars replaced by const and let
  • 5443136 chore(.editorconfig) configuration file created
  • 7233ba6 chore(.eslintrc) parserOptions option added to use es6
  • 322ebf6 bugfix: 486 Change method to use deepStrictEqual.

See the full diff

FAQ and help

There is a collection of frequently asked questions. If those don’t help, you can always ask the humans behind Greenkeeper.


Your Greenkeeper Bot 🌴

An in-range update of koa is breaking the build 🚨

Version 2.4.0 of koa was just published.

Branch Build failing 🚨
Dependency koa
Current Version 2.3.0
Type devDependency

This version is covered by your current version range and after updating it in your project the build failed.

koa is a devDependency of this project. It might not break your production code or affect downstream projects, but probably breaks your build or test tools, which may prevent deploying or publishing.

Status Details
  • ❌ continuous-integration/travis-ci/push The Travis CI build could not complete due to an error Details

Commits

The new version differs by 27 commits.

  • 418bb06 2.4.0 – added missing 2.3.0 changelog
  • c68a696 2.3.0
  • 687b732 travis: test node@9
  • 53a4446 expose the Application::handleRequest method (#950)
  • 85ff544 deps: update min engines (#1040)
  • 6029064 HTTP/2 has no status message (#1048) (#1049)
  • 18e4faf Update fresh to ^0.5.2 to close vulnerability (#1086)
  • e8a024c docs: ddd Chinese docs link for v2.x (#1092)
  • 1e81ea3 docs: update babel setup (#1077)
  • 43a1df8 test: Remove --forceExit flag for Jest (#1071)
  • 0168fd8 docs: Update middleware.gif (#1052)
  • e1e030c docs: command is wrong for running tests (#1065)
  • 77ca429 test: replace request(app.listen()) with request(app.callback())
  • 7f577af meta: update AUTHORS (#1067)
  • f3ede44 docs: fix dead link to logo image (#1069)

There are 27 commits in total.

See the full diff

FAQ and help

There is a collection of frequently asked questions. If those don’t help, you can always ask the humans behind Greenkeeper.


Your Greenkeeper Bot 🌴

Support for KOA 2.0-alpha

I'm wondering if is there any plan to release a version of this middleware for KOA 2.0.

In case that there isn't I may try to do the implementation the following days

Add CORS support

Add CORS support allowing to set per-route access controls and automatically setting the METHODS

An in-range update of mocha is breaking the build 🚨

Version 3.4.0 of mocha just got published.

Branch Build failing 🚨
Dependency mocha
Current Version 3.3.0
Type devDependency

This version is covered by your current version range and after updating it in your project the build failed.

As mocha is β€œonly” a devDependency of this project it might not break production or downstream projects, but β€œonly” your build or test tools – preventing new deploys or publishes.

I recommend you give this issue a high priority. I’m sure you can resolve this πŸ’ͺ

Status Details
  • ❌ continuous-integration/travis-ci/push The Travis CI build failed Details

Release Notes v3.4.0

Mocha is now moving to a quicker release schedule: when non-breaking changes are merged, a release should happen that week.

This week's highlights:

  • allowUncaught added to commandline as --allow-uncaught (and bugfixed)
  • warning-related Node flags

πŸŽ‰ Enhancements

πŸ› Fixes

πŸ”© Other

Commits

The new version differs by 9 commits0.

  • 7554b31 Add Changelog for v3.4.0
  • 9f7f7ed Add --trace-warnings flag
  • 92561c8 Add --no-warnings flag
  • ceee976 lint test/integration/fixtures/simple-reporter.js
  • dcfc094 Revert "use semistandard directly"
  • 93392dd no special case for macOS running Karma locally
  • 4d1d91d --allow-uncaught cli option
  • fb1e083 fix allowUncaught in browser
  • 4ed3fc5 Add license report and scan status

false

See the full diff

Not sure how things should work exactly?

There is a collection of frequently asked questions and of course you may always ask my humans.


Your Greenkeeper Bot 🌴

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.