1.3.21 • Published 5 months ago

auto-roots v1.3.21

Weekly downloads
-
License
MIT
Repository
github
Last release
5 months ago

auto-roots

this is a router for "koa" and "express" servers
that creates root automatically!

license

Table of Contents

Features

  • Automatic root generation
  • Protected router from an accidental call
  • async/await support

Install

npm install auto-roots

Description

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  

Koa Exemple

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.

Express Exemple

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.

Future

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.

License

MIT © Kirill Kukuliev

Thank's a million!

1.3.2

5 months ago

1.3.1

5 months ago

1.3.21

5 months ago

1.2.0

8 months ago

1.3.0

5 months ago

1.2.1

8 months ago

1.1.1

1 year ago

1.1.2

1 year ago

1.1.0

1 year ago

1.0.4

1 year ago

1.0.3

1 year ago

1.0.2

1 year ago

1.0.1

1 year ago

1.0.0

1 year ago