nnn-router
TypeScript icon, indicating that this package has built-in type declarations

1.1.7 • Public • Published

nnn-router

nnn-router is library for dynamic nested routes

Installation

npm install nnn-router

or with yarn

yarn add nnn-router

Usage

Example:

const nnn = require('nnn-router')
const express = require('express')
const app = express()
const router = express()

const options = {
  routeDir: '/routes', // DEFAULT '/routes'
  absolutePath: 'YOUR ABSOLUTE PATH', // NOT RQUIRED
  baseRouter: router   // NOT RQUIRED
}

app.use(nnn(options))

When you use both of routeDir and absolutePath, absolutePath overrides routeDir

This file tree:

routes/
--| users/
-----| post.js
-----| middleware.js
-----| _id/
-------| get.js
--| books/
-----| _bookId/
--------| authors/
-----------| _authorId/
-------------| get.js
--| get.js

generate express Route path:

/users/
/users/:id
/books/:bookId/authors/:authorId
/

each js file's form

exports.get = (req, res) => {
  res.send('nnn-router')
}

or

module.exports = (req, res) => {
  res.send('nnn-router')
}

Use middlewares:

Using under method

// get.js
exports = module.exports = (req, res) => {
  res.send('req.params.id is ' + req.params.id)
  console.log(req.params.id)
}

const middle = (req, res, next) => {
  console.log(req.method)
  next()
}

const middle2 = (req, res, next) => {
  console.log('bar')
  next()
}

exports.middleware = [middle, middle2]

Using under any file

// middleware.js
exports.middleware01 = (req, res, next) {
  cosole.log('middleware01')
  next()
}
exports.middleware02 = (req, res, next) {
  cosole.log('middleware02')
  next()
}

If use middleware overall, should set it the execution file

License

MIT

Readme

Keywords

none

Package Sidebar

Install

npm i nnn-router

Weekly Downloads

20

Version

1.1.7

License

MIT

Unpacked Size

7.69 kB

Total Files

5

Last publish

Collaborators

  • hirokinagase