# autoroute-express-promise

> autoroute for use with promises

Latest version **0.2.3** (published 2015-08-10) · ISC license · 0 weekly downloads

## Install

```sh
npm install autoroute-express-promise
pnpm add autoroute-express-promise
yarn add autoroute-express-promise
bun add autoroute-express-promise
```

## Health

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

Positive: no vulnerabilities.

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

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.2.3 |
| Published | 2015-08-10 |
| First published | 2015-06-25 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 4 |
| Known vulnerabilities | 0 (+6 in 1 direct dependencies) |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Jon Nyman |
| Maintainers | jon49 |
| Keywords | autoroute, auto-route, routing, route, controller, promise, express |

## Links

- npm: https://www.npmjs.com/package/autoroute-express-promise
- Repository: https://github.com/jon49/autoroute-promise
- Issues: https://github.com/jon49/autoroute-promise/issues
- npm.io page: https://npm.io/package/autoroute-express-promise

## Dependencies (4)

- [ramda](https://npm.io/package/ramda.md) ^0.17.1
- [lodash](https://npm.io/package/lodash.md) ^3.9.3
- [route-methods](https://npm.io/package/route-methods.md) 0.0.2
- [autoroute-base](https://npm.io/package/autoroute-base.md) 0.0.2

## Alternatives

- [express-promise-router](https://npm.io/package/express-promise-router.md) — 736.1K weekly downloads
- [next-usequerystate](https://npm.io/package/next-usequerystate.md) — 29.8K weekly downloads
- [@bitkyc08/opencodex](https://npm.io/package/@bitkyc08/opencodex.md) — 4.6K weekly downloads
- [lynkr](https://npm.io/package/lynkr.md) — 575 weekly downloads
- [baremetal.js](https://npm.io/package/baremetal.js.md) — 42 weekly downloads

## Recent versions

- 0.2.3 (latest) — 2015-08-10
- 0.2.2 — 2015-08-10
- 0.2.1 — 2015-08-10
- 0.1.1 — 2015-07-03
- 0.1.0 — 2015-06-26
- 0.0.1 — 2015-06-26
- 0.0.0 — 2015-06-25

## README

# Auto-route Express Promise

## Summary

`Auto-route Express Promise` routing made simple with a single place to update.

## Installation

Install with `npm`:

```
npm install --save autoroute-express-promise
```

## [Project Status](http://www.walkercoderanger.com/blog/2015/06/advice-for-open-source-projects/)

- Beta
- Active (June 26, 2015)

## Example

### Route Set Up

```js
import {routes} from 'autoroute-express-promise'
import isUserAuthenticated = require('../controllers/auth/auth')
import express = require('express')
import withMessageAs = require('../utilities/response-structure')

var router = express.Router()

var authenticatedRoute = route => router.route(route).all(isUserAuthenticated)

routes({
        baseRoute: authenticatedRoute,
        response: (response, result) => response.send(withMessageAs(result)),
        message: (o) => {console.log(o.routeName, o.methodName)}
    }, ['./controllers/**/index.js'])

module.exports = router
```

### Route/Controller Set Up

**File Structure**:

Note, that this is how I do my file structure. Any file structure is OK.

```
├── controllers/
    ├── api1/
    │   └── index.js
    ├── api2/
    │   └── index.js
    └── api3/
        └── index.js
```

**Code Structure**:

Note that you can do an array of routes also.

```js
import {method} from 'autoroute-express-promise'
import {getPet, petDied} = require('../../pets')

const PETS = '/pets',
      ID = 'petId',
      PET = PETS + '/:' + ID

var routes: AutoRouteExpressPromise.RouteDefinition = {
    route: PET,
    methods: [
        [ method.get,       (req: Request) => getPet(req.params[ID]) ],
        [ method.delete,    (req: Request) => petDied(req.params[ID]) ] ]
}

export = routes
```

Cleaner nested route syntax (version 0.2):

```js
var routes = {
    '/pets': {
        _methods: [
            [ method.post,      (req: Request) => getPet(req.body) ] ]
        ':id': [
            [ method.get,       (req: Request) => getPet(req.params[ID]) ],
            [ method.delete,    (req: Request) => petDied(req.params[ID]) ] ]
    },
    '/cats': {
        ':id': [
            [ method.get,       (req: Request) => getCat(req.params[ID]) ] ]
        '/hamsters': { // Overrides cats because of the forward slash at beginning
            '/fish': [ // Overrides hamsters (hamsters bite)
                [ method.get,   (req: Request) => getFish(req.params[ID])] ]
        }
    }
}
```

Example with two routes (this is the old way, you can still do it this way.):

```js
import {method} from 'autoroute-express-promise'
import {getPet, newPet, petDied} = require('../../pets')

const PETS = '/pets',
      ID = 'petId',
      PET = PETS + '/:' + ID

var routes: AutoRouteExpressPromise.RouteDefinition[] = [
    { route: PETS,
      methods: [
          [ method.post,       (req: Request) => getPet(req.body) ] ]
    },
    { route: PET,
      methods: [
          [ method.get,        (req: Request) => getPet(req.params[ID]) ],
          [ method.delete,     (req: Request) => petDied(req.params[ID]) ] ]
    }
]

export = routes
```
## API

```js
import {routes, method} from 'autoroute-express-promise'
```

### `routes(options, glob[])`:

where `options`:

```js
{
    baseRoute: (routeName: string) => any
    message?: (options: {routeName: string; methodName: string}) => void
    response: (client: Express.Response, result: any) => any
}
```

**baseRoute**: (Required) Uses route name (e.g., `/api/myroute/:id`) and return
an express.js `Router`. E.g.,

```js
var router = express.Router
var authenticatedRoute = route => router.route(route).all(isUserAuthenticated)
```

**message**: (Optional) A function which passes the route name and method name (post, get,
etc). Used for writing logs when route is called by server, e.g.,

```js
o => {console.log(o.routeName, o.methodName)}
```

where: `o` is `{routeName: '/api/myroute/:id', methodName: 'get'}`.

**response**: (Required) Used to wrap the result in some wrapper and send
method, e.g.,

```js
(client, result) => client.send({result: result})
```

where `glob`:

List of globs as specified in
[`require-glob`](https://www.npmjs.com/package/require-glob). E.g.,
`['./controllers/**/index.js']`.

### `method`:

This is an enumeration of all the `express.js` methods:

`get, post, put, head, delete, options, trace, copy, lock, mkcol, move, purge, propfind, proppatch, unlock, report, mkactivity, checkout, merge, "m-search", notify, subscribe, unsubscribe, patch, search, connect`

E.g.,

```js
method.get // => 0
method[0] // => "get"
```

---
_Source: https://npm.io/package/autoroute-express-promise · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
