Skip to content

it is a router from koa and express servers with suport of roots from fs path

License

Notifications You must be signed in to change notification settings

keha12345/fs-router

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

34 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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!

About

it is a router from koa and express servers with suport of roots from fs path

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published