Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/dotcypress/micro-route
Tiny and super fast http routing helper
https://github.com/dotcypress/micro-route
Last synced: 13 days ago
JSON representation
Tiny and super fast http routing helper
- Host: GitHub
- URL: https://github.com/dotcypress/micro-route
- Owner: dotcypress
- License: mit
- Created: 2016-12-03T06:06:51.000Z (almost 8 years ago)
- Default Branch: master
- Last Pushed: 2017-11-16T04:13:39.000Z (almost 7 years ago)
- Last Synced: 2024-05-08T08:32:34.002Z (6 months ago)
- Language: JavaScript
- Homepage:
- Size: 12.7 KB
- Stars: 94
- Watchers: 4
- Forks: 3
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-micro - micro-route - Tiny http routing helper. (Modules / Routing)
README
[![NPM Version](https://img.shields.io/npm/v/micro-route.svg?style=flat-square)](https://www.npmjs.com/package/micro-route)
[![node](https://img.shields.io/node/v/micro-route.svg?style=flat-square)](https://www.npmjs.com/package/micro-route)
[![Build Status](https://img.shields.io/travis/dotcypress/micro-route.svg?branch=master&style=flat-square)](https://travis-ci.org/dotcypress/micro-route)
[![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat-square)](http://standardjs.com/)# micro-route
> 🎛 Tiny http routing helper based on [`url-pattern`](https://github.com/snd/url-pattern)## Installation
Install from NPM:
```js
$ npm install micro-route --save
```## Examples
```js
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
}
}
``````js
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)
}
``````js
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) => ... )
```