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.2.1
  • 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 url pattern that you can define your path. In this path you can set your parameters using a : notation. The req parameter from handler will return this parameters as a object.

For more information about how you can define your path, see url-pattern, that's the package that we can use to match your paths.

handler

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

req.params

As you can see below, the req parameter has a property called params that represent the parameters that you defined in yout path:

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

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

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

console.log(response)  // { who: 'World' }
req.query

The req parameter also has a query property, that represent the queries defined in your requision url:

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

// service.js
module.exports = router(
  get('/user', (req, res) => req.query)
)

// test.js
const response = await request('/user?id=1')

console.log(response)  // { id: 1 }
req.body

Finally, the last property of req parameter that you might use a lot is body, that represent your body requisition passed through your method. Note that body result is the same that you will sent, here the body hasn't any parser, if you can parse it you need to do that by yourself

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

// service.js
module.exports = router(
  post('/user', (req, res) => req.body)
)

// test.js
const body = { name: 'John' }
const response = await request.post('/user', { body, json: true })

console.log(response)  // { name: 'John' }

🕺   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 02 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