JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 1
  • Score
    100M100P100Q11644F
  • 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').koa
Get the middleware function router(path, __dirname)

index.js

const Koa = require("koa");
const cors = require("@koa/cors");
const router = require('auto-roots').koa;
const app = new Koa();

app.use(parser())
  .use(cors())
  .use(router('routers',__dirname))
  .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').Router
This is done as a prevention of errors causing vulnerabilities.

controller.js

const Router = require('auto-roots').Router

module.exports = new Router({
        get: (cxt) => {return `hello ${cxt.method}`},
        post: async (cxt) => {
            await cxt.db.model.create(object)
            return 'ok'
        }
    },
    (cxt) => { if(!cxt.session) throw Error('auth failed')}
)

Express Exemple

Get middleware generation function const router = require('auto-roots').express
Get the middleware function router(path, __dirname)

index.js

const express = require("express");
const cors = require("cors");
const router = require('auto-roots').express;
const app = express();

app.use(parser());
app.use(cors())
app.use(router('routers',__dirname))
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').Router
This is done as a prevention of errors causing vulnerabilities.

controller.js

const Router = require('auto-roots').Router

module.exports = new Router({
        get: (req, res) => {return `hello ${cxt.method}`},
        post: async (req, res) => {
            await model.create(object)
            return 'ok'
        }
    },
    (req, res) => { if(!req.session) throw Error('auth failed')}
)

Future

An important drawback that can be seen today is that the params are not available.
Use queries or http headers.
This option will be added in future updates.

License

MIT © Kirill Kukuliev

Thank a million!