# express-route-keeper

> express route middleware for acls and parameter checker

Latest version **0.1.7** (published 2017-03-01) · MIT license · 0 weekly downloads

## Install

```sh
npm install express-route-keeper
pnpm add express-route-keeper
yarn add express-route-keeper
bun add express-route-keeper
```

## 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.1.7 |
| Published | 2017-03-01 |
| First published | 2016-04-04 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 3 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Chia Yu Pai |
| Maintainers | fantasywind |
| Keywords | express, acl, parameter, route, validation |

## Links

- npm: https://www.npmjs.com/package/express-route-keeper
- Repository: https://github.com/fantasywind/express-route-keeper
- Issues: https://github.com/fantasywind/express-route-keeper/issues
- npm.io page: https://npm.io/package/express-route-keeper

## Dependencies (3)

- [debug](https://npm.io/package/debug.md) ^2.2.0
- [lodash](https://npm.io/package/lodash.md) ^4.7.0
- [core-js](https://npm.io/package/core-js.md) ^2.2.1

## 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.1.7 (latest) — 2017-03-01
- 0.1.6 — 2017-02-28
- 0.1.5 — 2017-02-28
- 0.1.4 — 2016-10-25
- 0.1.3 — 2016-09-20
- 0.1.2 — 2016-09-20
- 0.1.1 — 2016-04-29
- 0.1.0 — 2016-04-04

## README

# Express Route Keeper

__Important!__ This project is in development, README.md is draft API.

This project is a route middleware for acls and parameter checker.

## Installation

```
npm i express-route-keeper
```

## Usage

```javascript
import express from 'express';
import bodyParser from 'body-parser';
import RouteKeeper from 'express-route-keeper';

const app = express();
const keeper = RouteKeeper();

const READ_PROJECT = Symbol('READ_PROJECT');
const CREATE_PROJECT = Symbol('CREATE_PROJECT');
const PROJECT_MASTER = Symbol('PROJECT_MASTER');

// middleware for request body parse
app.use(bodyParser.urlencoded({
  extended: true,
}));
app.use(bodyParser.json());

// acl role injection
app.use((req, res, next) => {
  req.aclActions = [
    READ_PROJECT,
    CREATE_PROJECT,
    PROJECT_MASTER
  ];

  next();
});

// public route
app.get('/news', routeHandler);

// acl route
app.get('/projects', keeper(READ_PROJECT), routeHandler);
app.get('/projects', keeper([
  READ_PROJECT,
  PROJECT_MASTER
]), routeHandler); // AND match

// parameter checker
app.post('/login', keeper({
  username: String,
  password: String,
  memorize: {
    type: Boolean,
    defaultValue: false,
    required: false,
  },
}), routeHandler);

// mixed mode
app.post('/projects', keeper({
  acl: [
    CREATE_PROJECT,
    PROJECT_MASTER
  ],
  name: String,
}), routeHandler);

app.listen(PORT);
```

### ACL OR match

```javascript
app.post('/projects', ({
  acl: {
    actions: [
      CREATE_PROJECT,
      PROJECT_MASTER
    ],
    $or: true,
  },
  name: String,
}), routeHandler);
```

### Working with other middleware

```javascript
// General Usage
// Check ACL before uploaded file handler
app.post('/projects', keeper(CREATE_PROJECT), multer.single('cover'), routeHandler);

// Body field checker with multipart
app.post('/projects', multer.single('cover'), keeper({
  name: String,
  managerId: Number,
  acl: [
    CREATE_PROJECT
  ],
}), routeHandler);
```

### Set custom exception

```javascript
import RouteKeeper, {
  PARAMS_ERROR,
  ACL_ERROR
} from 'express-route-keeper';

const keeper = RouteKeeper((err, req, res) => {
  switch (err) {
    case PARAMS_ERROR:
      res.status(401);
      res.json({
        message: 'Invalid Parameter',
      });
      break;

    case ACL_ERROR:
      res.status(401);
      res.json({
        message: 'Authentication Failed',
      });
      break;

    default:
      console.error(err);

      res.status(500);
      res.json({
        message: 'Server Error'
      });
      break;
  }
});
```

### Action Keys

You can use String or ES6 Symbol (Recommend) for action keys.

### Querystring or Body

If request method is __GET__, check the querystring in ```req.query``` or check parameters in ```req.body```.

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