koa-load-routes
Koa middleware to load routes from files and directories using koa-router module
Warning
- Node.js 8.0 and koa@2.x or latest are required to use this module.
- Since v2.0 koa-load-routes not support Generator Functions anymore (in favor of Async Functions).
- Since v3.0 path option must be a absolute path
- Since v3.0 firstMiddleware option was removed in favor of middlewares option.
Installation
$ npm install --save koa-load-routes
or
$ yarn add koa-load-routes
Options
-
String -> path (required)
- Path from where the module will try to load the routes, if is a file, it will read the routes defined just inside of the file, otherwise, if is a directory, will read all .js files in the directory and load the routes inside each one.
-
Boolean -> recursive (optional, default: false)
- If this argument is true the module will search route files recursive trhoug directories.
-
Array[Function|AsyncFunction] -> middlewares (optional)
- A list of middlewares which will be added to each route loaded, can be Array of async functions or a normal functions or mixed, this middlewares will be attached previous to each route, useful by example for authentication middlewares.
-
String -> base (optional)
- Adds the "base" string at the start of each loaded route url.
-
Array[any] -> args (optional)
- Arguments to be pased inside the route main function, each array elements are passed as a independent argument (see examples below).
-
String -> suffix (optional, default: '.js')
- If this argument is passed the module will load only the files which ends with the passed suffix + .js, by default if suffix is not supplied will try to load all files with .js extension.
Usage
app.js
const Koa = ;const loader = ;const authMiddleware = ;const path = ; // Some module containing business logic and db accessconst Interactors = ; var app = ; // Loading public routesapp; // Loading private routes with auth middlewareapp; app;
src/resources/public/login.route.js
module { // Authentication endpoint this; // Other possible routes examples this; // Routes chain this ; // Multiple middlewares this;};
License
MIT © Gonzalo Bahamondez