1.1.6 • Published 2 years ago

nnn-router v1.1.6

Weekly downloads
31
License
MIT
Repository
github
Last release
2 years ago

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

1.1.6

2 years ago

1.1.5

2 years ago

1.1.4

2 years ago

1.1.3

3 years ago

1.1.2

3 years ago

1.1.1

3 years ago

1.1.0

3 years ago

1.0.7

4 years ago

1.0.6

4 years ago

1.0.5

5 years ago

1.0.4

5 years ago

1.0.3

5 years ago

1.0.2

5 years ago

1.0.1

5 years ago

1.0.0

5 years ago

0.0.3

5 years ago

0.0.2

5 years ago

0.0.1

5 years ago