# restify-router-config

> The laziest way to route your restify app.

Latest version **1.2.0-alpha.0** (published 2018-07-28) · ISC license · 0 weekly downloads

## Install

```sh
npm install restify-router-config
pnpm add restify-router-config
yarn add restify-router-config
bun add restify-router-config
```

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.2.0-alpha.0 |
| Published | 2018-07-28 |
| First published | 2018-03-17 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 12.5 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Maintainers | yakovmeister |
| Keywords | restify, restify-router, express, express-router-config, express-router, router, config, react, inspired |

## Links

- npm: https://www.npmjs.com/package/restify-router-config
- npm.io page: https://npm.io/package/restify-router-config

## Alternatives

- [mobx-react](https://npm.io/package/mobx-react.md) — 2.8M weekly downloads
- [rc-tree](https://npm.io/package/rc-tree.md) — 2.6M weekly downloads
- [@react-oauth/google](https://npm.io/package/@react-oauth/google.md) — 1.3M weekly downloads
- [@wagmi/connectors](https://npm.io/package/@wagmi/connectors.md) — 877.0K weekly downloads
- [vee-validate](https://npm.io/package/vee-validate.md) — 836.4K weekly downloads

## Recent versions

- 1.2.0-alpha.0 (latest) — 2018-07-28
- 1.1.5 — 2018-07-23
- 1.1.4 — 2018-07-22
- 1.1.3 — 2018-07-22
- 1.1.1 — 2018-07-21
- 1.0.0 — 2018-07-20
- 0.3.4 — 2018-07-11
- 0.3.3 — 2018-07-11
- 0.3.2 — 2018-07-03
- 0.3.1 — 2018-07-03
- 0.3.0 — 2018-07-03
- 0.2.2 — 2018-06-18
- 0.2.0 — 2018-06-17
- 0.1.4 — 2018-03-18
- 0.1.3 — 2018-03-18
- … 3 more at https://npm.io/package/restify-router-config/versions

## README

# restify-router-config
> The laziest way to route your restify app.

[![Build Status][travis-image]][travis-url]
[![NPM Version][npm-image]][npm-url]
[![Downloads Stats][npm-downloads]][npm-url] 
  
This is an effort to maximize laziness in routing restify app. This tool includes grouping of routes, sorting of routes, and applying multiple middleware in most convenient way, of course the structure is inspired by [react-router-config](https://www.npmjs.com/package/react-router-config).  
  
NOTE: This module is also compatible with [express](https://expressjs.com/) but usage may vary.

## Features: 
  
* Sorting - will sort your routes, wildcards has been taken into account too.
* Grouping - will add a prefix to your routes depending on the group.
* Multiple Middleware - will let you add one or more middleware in most convenient way.
* Pre/Post Middleware - let's you define whether the middleware should execute before or after executing your function
* Nesting groups - allows you to nest your groups.
  
## Installation
  
```
npm install -S restify-router-config
```  
  
## Usage
  
```javascript
import * as restify from 'restify'
import router from 'restify-router-config'
import { getUserById, getUser, login } from './controllers/users'
import { restrictedRoute } from './middlewares'

/** create restify server */
const server = restify.createServer()
/** configure your routes */
// NOTE: you may or may not use group, but for the sake of grouping up
// your endpoints it's best to use it.
router(server)([
  {
    group: 'users',
    routes: [
      {
        match: '/:id',                  // would generate /users/:id route
        middleware: restrictedRoute,
        method: 'get',
        action: getUserById
      },
      {
        match: '/',                     // would generate /users
        method: 'get',
        action: getUser
      }
    ]
  },
  {
    match: '/login',                    // would generate login
    method: 'post',
    action: login
  }
])

server.listen(8080)
```  

### Nesting groups

```javascript
const router = require('restify-router-config')
const restify = require('restify')

const apiAuth = (req, res, next) => {
  console.log('authed!'); 
  next()
}

const loggingMW = (req, res, next) => {
  console.log(req._timeStart)

  next()
}

const logDone = (req, res, next) => {
  console.log('done!')

  next()
}


router(server) ([
  {
    group: 'api/v1',
    middleware: apiAuth,
    routes: [
      {
        match: '/hello',
        method: 'get',
        action: (req, res, next) => res.send('hello')
      },
      {
        group: 'users',
        middleware: [
          ['before', loggingMW],
          ['after', logDone]
        ],
        routes: [
          {
            match: '/:id',
            method: 'get',
            action: (req, res, next) => {
              res.send('hello')

              next()
            }
          },
          {
            match: '/:id/friends',
            method: 'get',
            action: (req, res, next) => {
              res.send('hello')

              next()
            }
          },
          {
            match: '/',
            method: 'get',
            action: (req, res, next) => {
              res.send('hello')

              next()
            }
          }
        ]
      }
    ]
  }
])

server.listen(4000)
```  
The example above would generate the following routes:
```
[get] - /api/v1/users/:id/friends
[get] - /api/v1/users/:id
[get] - /api/v1/hello
[get] - /api/v1/users
```

### Setting middleware's execution

You may choose to add middleware after the function's execution, in order to do that you must use the following syntax:

```javascript
import * as restify from 'restify'
import router from 'restify-router-config'
import { getUserById } from './controllers/users'
import { authenticate, logger } from './middlewares'

/** create restify server */
const server = restify.createServer()
/** configure your routes */
// NOTE: you may or may not use group, but for the sake of grouping up
// your endpoints it's best to use it.
router(server)([
  {
    match: '/users/:id',
    method: 'get',
    middleware: [
      ['before', authenticate],
      ['after', logger]
    ]
    action: getUserById
  }
])

server.listen(8080)
```
in the example above, the `/users/:id` route is authenticated first, then executes `getUserById`, after the execution of the `getUserById` the `logger` is executed right away.

  
## Why use restify-router-config?  
  
You don't have to, but if you're used to `react-router-config`, then this would make it easier for you to configure your routes as 
it is almost similar structure to `react-router-config`, another advantage of using this route tool is you can easily chain your middleware. For instance if you want to protect all of your routes, and at same time a single route would require additional middleware, you'd do it like this:  

```javascript
import { anotherMiddleware } from './middlewares'

router(server)([
  {
    // assuming that you want to protect all routes under /users
    group: 'users',
    middleware: restrictedRoute,
    routes: [
      {
        match: '/:id',
        middleware: anotherMiddleware,
        method: 'get',
        action: getUserById
      },
      {
        match: '/',
        method: 'get',
        action: getUser
      }
    ]
  }
])
```
the code from above would use `restrictedRoute` middleware first, and if you're going to access /users/:id, 
it would also use `anotherMiddleware` middleware.  


[npm-image]: https://img.shields.io/npm/v/restify-router-config.svg?style=flat-square
[npm-url]: https://npmjs.org/package/restify-router-config
[npm-downloads]: https://img.shields.io/npm/dm/restify-router-config.svg?style=flat-square
[travis-image]: https://travis-ci.org/yakovmeister/restify-router-config.svg?branch=dev
[travis-url]: https://travis-ci.org/yakovmeister/restify-router-config

---
_Source: https://npm.io/package/restify-router-config · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
