micro-route

2.5.0 • Public • Published

NPM Version node Build Status js-standard-style

micro-route

🎛 Tiny http routing helper based on url-pattern

Installation

Install from NPM:

$ npm install micro-route --save

Examples

const route = require('micro-route')
 
const corsRoute = route('*', 'OPTIONS')
const fooRoute = route('/', ['POST', 'PUT'])
const barRoute = route('/api/collection/:id', 'DELETE')
const anotherRoute = route('/api/transactions/:id')
 
module.exports = function (req, res) {
  if (corsRoute(req)) {
    // Send CORS headers 
  } else if (fooRoute(req)) {
    // Do cool stuff
  }
}
const match = require('micro-route/match')
 
module.exports = function (req, res) {
  const { params, query } = match(req, '/api/transactions/:id?ts=12', true)
  console.log('Transaction id:', params.id)  
  console.log('ts:', query.ts)  
}
const dispatch = require('micro-route/dispatch')
 
module.exports = dispatch()
  .dispatch('*', 'OPTIONS', (req, res) => ... )
  .dispatch('/', ['POST', 'PUT'], (req, res) => ... )
  .dispatch('/api/collection/:id', 'DELETE', (req, res) => ... )
  .dispatch('/api/transactions/:id', '*', (req, res, { params, query }) => ... )
  .otherwise((req, res) => ... )

Package Sidebar

Install

npm i micro-route

Weekly Downloads

1,600

Version

2.5.0

License

MIT

Last publish

Collaborators

  • dotcypress