Skip to content

Latest commit

 

History

History
55 lines (43 loc) · 1.7 KB

README.md

File metadata and controls

55 lines (43 loc) · 1.7 KB

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) => ... )