JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 1
  • Score
    100M100P100Q11633F
  • License MIT

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

Package Exports

  • auto-roots
  • auto-roots/index.js

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (auto-roots) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

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!