1.0.0 • Published 8 years ago

stackkit-advanced-router v1.0.0

Weekly downloads
-
License
MIT
Repository
github
Last release
8 years ago

stackkit-advanced-router

First try on replicating the laravel router.

Setup

1. Install the router with npm or yarn

# npm
npm install stackkit-advanced-router

2. Then setup the router in your app.

const express = require('express')
const advancedrouter = require('../dist/index')
const app = express()

const router = advancedrouter.use(app)

router.listen(3000)

Docs

Router Group

You can create route groups with this router. This will name space all the routes with the given name space

router.group('/api/v1', (router) => {
  router.get('/test', (req, res) => res.send('hallo?'))
}

Router Resource

You can auto create routes for resource controller. By using the resource function.

router.resource('name/route', require('controller'), 'options')

Only

This option will create only the given resource routes.

router.resource('example', require('controller'), { only: ['index', 'show'] })

This will result in the following routes:

  • example/ -> to controller index
  • example/:id -> to controller show

Except

This option gives you the option to remove resources form the autocreation process.

router.resource('example', require('controller'), { except: ['create', 'store', 'destroy'] })

This will result in the following routes:

  • /example -> to controller index
  • /example/:id -> to controller show

Identifier

This option gives you the option to set a custom identifier. The default of this is 'id'. You can enter this by using req.params.id in your controller.

router.resource('example', require('controller'), { identifier: '_id' })

This will result in the following routes:

  • /example -> to controller index
  • /example/:id -> to controller show

Router Debug

This router can show witch routes it has created. Add at the and of the router this code:

router.debug()

Example output

/api/v1/3 | get
/api/v1/3 | post
/api/v1/3 | put
/api/v1/3 | patch
/api/v1/3 | post
/api/v1/1 | get
/api/v1/1 | post
/api/v1/1/:id | get
/api/v1/1/:id | put
/api/v1/1/:id | delete
/api/v1/2 | get
/api/v1/2 | post

Example controller

module.exports = class userController {
  index (req, res) {
    res.json({
      'name': 'index',
      'succes': true
    })
  }

  store (req, res) {
    res.json({
      'name': 'store',
      'succes': true
    })
  }

  show (req, res) {
    res.json({
      'name': 'show',
      'succes': true,
      'id': req.params
    })
  }

  update (req, res) {
    res.json({
      'name': 'update',
      'succes': true,
      'id': req.params
    })
  }

  destroy (req, res) {
    res.json({
      'name': 'destroy',
      'succes': true,
      'id': req.params
    })
  }
}
1.0.0

8 years ago

0.0.2

8 years ago

0.0.1

8 years ago