# express-coroutine

> Generator function and Async function to be used as middlewares in express like koa.

Latest version **2.4.0** (published 2018-07-25) · MIT license · 0 weekly downloads

## Install

```sh
npm install express-coroutine
pnpm add express-coroutine
yarn add express-coroutine
bun add express-coroutine
```

## 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 | 2.4.0 |
| Published | 2018-07-25 |
| First published | 2017-02-07 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 23.1 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 3 |
| Author | Yourtion |
| Maintainers | yourtion |
| Keywords | express, co, coroutine, generators |

## Links

- npm: https://www.npmjs.com/package/express-coroutine
- Repository: https://github.com/yourtion/express-coroutine
- Homepage: https://github.com/yourtion/express-coroutine#readme
- Issues: https://github.com/yourtion/express-coroutine/issues
- npm.io page: https://npm.io/package/express-coroutine

## Dependencies (1)

- [lei-coroutine](https://npm.io/package/lei-coroutine.md) ^1.6.3

## Alternatives

- [flatbuffers](https://npm.io/package/flatbuffers.md) — 6.0M weekly downloads
- [@exodus/patch-broken-hermes-typed-arrays](https://npm.io/package/@exodus/patch-broken-hermes-typed-arrays.md) — 28.5K weekly downloads
- [@native-to-anchor/buffer-layout](https://npm.io/package/@native-to-anchor/buffer-layout.md) — 12.2K weekly downloads
- [binary-parser-encoder](https://npm.io/package/binary-parser-encoder.md) — 5.3K weekly downloads
- [datasets-fi-postalcodes](https://npm.io/package/datasets-fi-postalcodes.md) — 1.1K weekly downloads

## Recent versions

- 2.4.0 (latest) — 2018-07-25
- 2.3.0 — 2018-03-26
- 2.2.0 — 2018-03-24
- 2.1.0 — 2017-12-18
- 2.0.2 — 2017-12-15
- 2.0.1 — 2017-12-15
- 2.0.0 — 2017-12-14
- 1.1.1 — 2017-04-26
- 1.1.0 — 2017-02-25
- 1.0.2 — 2017-02-11
- 1.0.1 — 2017-02-11
- 0.2.0 — 2017-02-08
- 0.1.0 — 2017-02-07

## README

[![NPM version][npm-image]][npm-url]
[![build status][travis-image]][travis-url]
[![Test coverage][coveralls-image]][coveralls-url]
[![David deps][david-image]][david-url]
[![node version][node-image]][node-url]
[![npm download][download-image]][download-url]
[![npm license][license-image]][download-url]
[![Greenkeeper badge](https://badges.greenkeeper.io/yourtion/express-coroutine.svg)](https://greenkeeper.io/)

[npm-image]: https://img.shields.io/npm/v/express-coroutine.svg?style=flat-square
[npm-url]: https://npmjs.org/package/express-coroutine
[travis-image]: https://img.shields.io/travis/yourtion/express-coroutine.svg?style=flat-square
[travis-url]: https://travis-ci.org/yourtion/express-coroutine
[coveralls-image]: https://img.shields.io/coveralls/yourtion/express-coroutine.svg?style=flat-square
[coveralls-url]: https://coveralls.io/r/yourtion/express-coroutine?branch=master
[david-image]: https://img.shields.io/david/yourtion/express-coroutine.svg?style=flat-square
[david-url]: https://david-dm.org/yourtion/express-coroutine
[node-image]: https://img.shields.io/badge/node.js-%3E=4.0-green.svg?style=flat-square
[node-url]: http://nodejs.org/download/
[download-image]: https://img.shields.io/npm/dm/express-coroutine.svg?style=flat-square
[download-url]: https://npmjs.org/package/express-coroutine
[license-image]: https://img.shields.io/npm/l/express-coroutine.svg

# express-coroutine

Generator function and Async function to be used as middlewares in express like koa. ( use [lei-coroutine](https://github.com/leizongmin/lei-coroutine) )

## Installation

```bash
npm install express-coroutine --save
```

## Usage

### Init express

```javascript
const express = require('express-coroutine')(require('express'));
const app = express();
```

### Init router

```javascript
const express = require('express-coroutine')(require('express'));
const app = express();
const router = new express.Router();
app.use(router);

router.get('/', function* (req, res) {
  res.send('it works!');
});

// also async function
router.get('/', async function (req, res) {
  res.send('it works!');
});
```

Write your express routes by using generator functions as middlewares. 

```javascript
const express = require('express-coroutine')(require('express'));

const app = express();

app.get('/user/:id', function* (req, res) {
  const user = yield User.findById(req.params.id);
  res.send(user);
})

app.get('/error', function* (req, res) {
  throw new Error('Bang!');
});

// also async function
app.get('/user2/:id', async function* (req, res) {
  const user = await User.findById(req.params.id);
  res.send(user);
});

app.listen(8000);
```

You can also define multiple generator functions just the express way.

```javascript
const fn = function (req, res, next) {
  return new Promise((resolve, reject) => {
    if (req.params.user !== 'a') return reject(new Error('Bang!'));
    resolve('a');
  });
};

const middleware = function* (req, res, next) {
  yield fn(req, res, next);
};

const middleware2 = async function (req, res, next) {
  await fn(req, res, next);
  next();
};

app.get('/:user', middleware, function* (req, res) {
  res.send('it works!');
});

app.get('/:user', middleware2, async function (req, res) {
  res.send('it works!');
});
```

More detail demo can be found in [test.js](test.js)

### Get coroutine

```javascript
const coroutine = require('express-coroutine').coroutine;
```

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