1.0.6 • Published 6 years ago

load-route-files v1.0.6

Weekly downloads
3
License
MIT
Repository
github
Last release
6 years ago

Load Route Files

A package to load all route files to then use them as middlewares. I built and tested with Koa, but as agnostic it is, probably can be implemented in any other framework.

Instalation

Just run:

  npm i load-route-files

Usage

In your project, you should have a folder with subfolders. Those folders should contain a routes.js file inside. In this example I use a folder called domains. So the structure should be at least the following:

.
├── ...
├── api.js
├── domains
│   ├── users
│   │   ├── routes.js
|   |   └── ...
│   └── permissions
|       ├── routes.js
|       └── ...
└── ...
  // in api.js
  const Koa = require('koa');
  const api = new Koa();

  const loadRoutes = require('load-route-files');
  const routes = await loadRoutes({ directory: 'domains' });

  const koaCompose = require('koa-compose'); //takes an array of milewares and compose them as one.

  api.use( koaCompose(routes) );

You can also specify a routeFilename in config, with is a custom name for the route file, if you wish.

1.0.6

6 years ago

1.0.5

6 years ago

1.0.4

6 years ago