# @lebretr/koa-router-enrouten

> An koa route initialization and configuration module.

Latest version **1.3.3** (published 2019-09-13) · 0 weekly downloads

## Install

```sh
npm install @lebretr/koa-router-enrouten
pnpm add @lebretr/koa-router-enrouten
yarn add @lebretr/koa-router-enrouten
bun add @lebretr/koa-router-enrouten
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.3.3 |
| Published | 2019-09-13 |
| First published | 2019-04-29 |
| Weekly downloads | 0 |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 3 |
| Unpacked size | 25.4 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Rémi Lebret |
| Maintainers | lebretr |
| Keywords | koa, koa-router, node, js, javascript, routing |

## Links

- npm: https://www.npmjs.com/package/@lebretr/koa-router-enrouten
- Repository: https://github.com/lebretr/koa-router-enrouten
- Homepage: https://github.com/lebretr/koa-router-enrouten#readme
- Issues: https://github.com/lebretr/koa-router-enrouten/issues
- npm.io page: https://npm.io/package/@lebretr/koa-router-enrouten

## Dependencies (3)

- [caller](https://npm.io/package/caller.md) ^1.0.0
- [debuglog](https://npm.io/package/debuglog.md) ^1.0.1
- [path-to-regexp](https://npm.io/package/path-to-regexp.md) ^1.1.1

## Alternatives

- [@mapbox/jsonlint-lines-primitives](https://npm.io/package/@mapbox/jsonlint-lines-primitives.md) — 5.3M weekly downloads
- [reftools](https://npm.io/package/reftools.md) — 3.5M weekly downloads
- [@hey-api/openapi-ts](https://npm.io/package/@hey-api/openapi-ts.md) — 3.5M weekly downloads
- [@mapbox/geojson-rewind](https://npm.io/package/@mapbox/geojson-rewind.md) — 2.4M weekly downloads
- [turbo-stream](https://npm.io/package/turbo-stream.md) — 1.7M weekly downloads

## Recent versions

- 1.3.3 (latest) — 2019-09-13
- 1.3.2 — 2019-05-09
- 1.3.1 — 2019-04-29

## README

# koa-router-enrouten

[![npm version](https://badge.fury.io/js/%40lebretr%2Fkoa-router-enrouten.svg)](https://badge.fury.io/js/%40lebretr%2Fkoa-router-enrouten)

Route configuration middleware for koa-router.


### API
#### `enrouten(options, router))`
```javascript
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.

```text
controllers
 |-user
     |-create.js
     |-list.js
```
```javascript
// create.js
module.exports = function (router) {
    router.post('/', function (ctx) {
        ctx.status=200;
        ctx.body('ok');
    });
};
```
```javascript
enrouten({
    directory: './controllers'
},router);
```
Routes are now:
```test
/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).
```javascript
enrouten({
    index: 'routes/'
},router);
```
```javascript
// 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.**

```javascript
// 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
```bash
$ npm test
```

## Coverage
```bash
$ npm run-script cover && open coverage/lcov-report/index.html
```

---
_Source: https://npm.io/package/@lebretr/koa-router-enrouten · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
