micro-route
v2.5.0
Published
Tiny http routing helper
Downloads
4,391
Readme
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) => ... )