1.3.3 • Published 5 years ago

@lebretr/koa-router-enrouten v1.3.3

Weekly downloads
-
License
-
Repository
github
Last release
5 years ago

koa-router-enrouten

npm version

Route configuration middleware for koa-router.

API

enrouten(options, router))

const Koa = require('koa')
    , app = new Koa()
    , Router = require('koa-router')
    , router = new Router()
    , enrouten = require('@lebretr/koa-router-enrouten');

enrouten({
    directory: './controllers'
},router);

app.use(router.routes())
    .use(router.allowedMethods());
...
// or app.use('/foo', enrouten({ ... }));

CONFIGURATION

directory

The directory configuration option (optional) is the path to a directory. Specify a directory to have enrouten scan all files recursively to find files that match the controller-spec API. With this API, the directory structure dictates the paths at which handlers will be mounted.

controllers
 |-user
     |-create.js
     |-list.js
// create.js
module.exports = function (router) {
    router.post('/', function (ctx) {
        ctx.status=200;
        ctx.body('ok');
    });
};
enrouten({
    directory: './controllers'
},router);

Routes are now:

/user/create
/user/list

index

The index configuration option (optional) is the path to the single file to load (which acts as the route 'index' of the application).

enrouten({
    index: 'routes/'
},router);
// index.js
module.exports = function (router) {

    router.get('/', index);
    router.all(passport.protect).get('/account', account);

    // etc...
};

Controller Files

A 'controller' is defined as any require-able file which exports a function that accepts a single argument. Any files with an extension of .js (or .coffee if CoffeeScript is registered) will be loaded and if it exports a function that accepts a single argument then this function will be called. NOTE: Any file in the directory tree that matches the API will be invoked/initialized with the koa-router object.

// Good :)
// controllers/controller.js
module.exports = function (router) {
    router.get('/', function (ctx) {
        // ...
    });
};

// Bad :(
// Function does not get returned when `require`-ed, use `module.exports`
exports = function (router) {
    // ...
};

// Bad :(
// controllers/other-file-in-same-controller-directory.js
modules.exports = function (config) {
    // `config` will be an koa-router
    // ...
};

## Linting
```bash
$ npm run-script lint

Tests

$ npm test

Coverage

$ npm run-script cover && open coverage/lcov-report/index.html
1.3.3

5 years ago

1.3.2

5 years ago

1.3.1

5 years ago

1.3.0

5 years ago