1.1.7 • Published 12 months ago
nnn-router v1.1.7
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.7
12 months ago
1.1.6
3 years ago
1.1.5
4 years ago
1.1.4
4 years ago
1.1.3
4 years ago
1.1.2
4 years ago
1.1.1
4 years ago
1.1.0
4 years ago
1.0.7
5 years ago
1.0.6
5 years ago
1.0.5
6 years ago
1.0.4
6 years ago
1.0.3
6 years ago
1.0.2
6 years ago
1.0.1
6 years ago
1.0.0
6 years ago
0.0.3
6 years ago
0.0.2
6 years ago
0.0.1
6 years ago