Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

microrouter

Package Overview
Dependencies
Maintainers
1
Versions
20
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

microrouter

:station: _**Micro Router -**_ A tiny and functional router for Zeit's [micro](https://github.com/zeit/micro)

  • 1.1.0
  • npm
  • Socket score

Version published
Weekly downloads
5.2K
decreased by-17.87%
Maintainers
1
Weekly downloads
 
Created
Source

:station: Micro Router - A tiny and functional router for Zeit's micro

GitHub release Build Status XO code style

👌   Features

  • Tiny. Just 23 lines of code.
  • Functional. Write your http methods using functions.
  • Async. Design to use with async/await

💻   Usage

Install as project dependency:

$ yarn add microrouter

Then you can define your routes inside your microservice:

const { send } = require('micro')
const { router, get } = require('microrouter')

const hello = (req, res) =>
  send(res, 200, `Hello ${req.params.who}`)

const notfound = () =>
  send(res, 404, 'Not found route')

module.exports = router(
  get('/hello/:who', hello),
  get('/*', notfound)
)

async/await

You can use your handler as an async function:

const { send } = require('micro')
const { router, get } = require('microrouter')

const hello = async (req, res) =>
  send(res, 200, await Promise.resolve(`Hello ${req.params.who}`))

module.exports = router(
  get('/hello/:who', hello)
)

route methods

Each route is a single basic http method that you import from microrouter and has the same arguments:

  • get(path, handler)
  • post(path, handler)
  • put(path, handler)
  • patch(path, handler)
  • delete(path, handler)
  • head(path, handler)
  • options(path, handler)
path

A simple route path that you can set any parameters using a : notation. The req parameter from handler will return this parameters as a object.

const { router, get } = require('microrouter')
const request = require('some-request-lib')

const hello = (req, res) => console.log(req.params) // { who: 'World' }

// service.js
module.exports = router(
  get('/hello/:who', hello)
)

// test.js
const body = await request('/hello/World')
handler

The handler method is simple function that will make some action base on your path. The format of this method is (res, res) => {}

🕺   Contribute

  1. Fork this repository to your own GitHub account and then clone it to your local device
  2. Install dependencies using Yarn: yarn install
  3. Make the necessary changes and ensure that the tests are passing using yarn test
  4. Send a pull request 🙌

FAQs

Package last updated on 01 Mar 2017

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc