this is a router for "koa" and "express" servers
that creates root automatically!
- Automatic root generation
- Protected router from an accidental call
async/await
support
npm install auto-roots
You can get automatic root controllers in the file system
routers
├――――――foo
│ ├―――bar.js
│ └――baz.js
└―――――other.js
you will get roots:
/foo/bar
/foo/baz
/other
Get middleware generation function const {router} = require('auto-roots')
Get the middleware function .use(router(__dirname+path'))
index.js
const Koa = require("koa");
const cors = require("@koa/cors");
const {router} = require('auto-roots');
const app = new Koa();
app.use(parser())
.use(cors())
.use(router(__dirname+'/roots'));
.listen(8000, () => {
console.log(`🚀 on port:${8000}`);
});
For the router to work, the controller file must return an instance of the Root, otherwise it will be skipped.
The router constructor can be obtained from require('auto-roots').Root
This is done as a prevention of errors causing vulnerabilities.
controller.js
const {Root} = require('auto-roots')
module.exports = new Root({
GET: [
middlewareFunction,
async (ctx,next) => {
ctx.body = hello;
await next();
},
anotherMiddlewareFunction
],
DELETE: [ async (ctx,next) => {
ctx.body = 'something deleted';
await next();
}]
},
[checkUserMiddleware, checkSomthingElse]
)
You can combine arrays or single functions inside one constructor.
Get middleware generation function const {router} = require('auto-roots')
Get the middleware function app.use(router(__dirname+path))
index.js
const express = require("express");
const cors = require("cors");
const {router} = require('auto-roots');
const app = express();
app.use(parser());
app.use(cors())
app.use(router(__dirname+'/roots'))
app.listen(8000, () => {
console.log(`🚀 on port:${8000}`);
});
For the router to work, the controller file must return an instance of the router, otherwise it will be skipped.
The router constructor can be obtained from require('auto-roots').Root
This is done as a prevention of errors causing vulnerabilities.
controller.js
const {Root} = require('auto-roots')
module.exports = new Root({
GET: [
middlewareFunction,
async (req,res,next) => {
req.sesion.hello = hello;
next();
},
sendHelloFunction
],
DELETE: [ async (req,res,next) => {
console.log('something deleted');
res.status(200).end()
}]
},
[checkUserMiddleware, checkSomthingElse]
)
You can combine arrays or single functions inside one constructor. You can use to sync controllers functions.
An important drawback that can be seen today is
that the params are not available.
Use query strings or http headers.
This option will be added in future updates.
MIT © Kirill Kukuliev
Thank's a million!