Socket
Book a DemoInstallSign in
Socket

@lebretr/koa-router-enrouten

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@lebretr/koa-router-enrouten

An koa route initialization and configuration module.

1.3.3
latest
Source
npmnpm
Version published
Weekly downloads
0
Maintainers
1
Weekly downloads
 
Created
Source

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

Keywords

koa

FAQs

Package last updated on 13 Sep 2019

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.