# koa-app-party

> koa app party

Latest version **0.1.10** (published 2015-11-19) · MIT license · 0 weekly downloads

## Install

```sh
npm install koa-app-party
pnpm add koa-app-party
yarn add koa-app-party
bun add koa-app-party
```

## 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.10 |
| Published | 2015-11-19 |
| First published | 2015-08-03 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 11 |
| Known vulnerabilities | 0 (+10 in 3 direct dependencies) |
| Install scripts | no |
| Author | Robin Qu |
| Maintainers | robinqu |

## Links

- npm: https://www.npmjs.com/package/koa-app-party
- npm.io page: https://npm.io/package/koa-app-party

## Dependencies (11)

- [koa](https://npm.io/package/koa.md) ~0.21.0
- [debug](https://npm.io/package/debug.md) ~2.2.0
- [catlog](https://npm.io/package/catlog.md) ^1.0.0
- [lodash](https://npm.io/package/lodash.md) ~3.10.0
- [bluebird](https://npm.io/package/bluebird.md) ~2.9.34
- [koa-mount](https://npm.io/package/koa-mount.md) ~1.3.0
- [next-port](https://npm.io/package/next-port.md) 0.0.5
- [koa-router](https://npm.io/package/koa-router.md) ~5.1.2
- [koa-compose](https://npm.io/package/koa-compose.md) ~2.3.0
- [cls-bluebird](https://npm.io/package/cls-bluebird.md) ~1.0.1
- [continuation-local-storage](https://npm.io/package/continuation-local-storage.md) ~3.1.4

## Recent versions

- 0.1.10 (latest) — 2015-11-19
- 0.1.9 — 2015-11-19
- 0.1.8 — 2015-11-17
- 0.1.7 — 2015-08-19
- 0.1.6 — 2015-08-18
- 0.1.5 — 2015-08-18
- 0.1.4 — 2015-08-16
- 0.1.2 — 2015-08-16
- 0.1.1 — 2015-08-16
- 0.1.0 — 2015-08-15
- 0.0.1 — 2015-08-03

## README

# koa-app-party

[![Build Status](https://travis-ci.org/RobinQu/koa-app-party.svg)](https://travis-ci.org/RobinQu/koa-app-party)


Enjoy the dark art of sub-app in koa

## Usage

### Mounting

```
var ap = require('koa-app-party');

var App1 = ap.extend(function() {
  this.use(require('./middleware/foo'));
});

var App2 = ap.extend(function() {
  this.use(require('./middleware/bar'));
});

var Root = ap.Container.extend(function() {
  this.mount('/foo', App1);
  this.mount('/bar', App2);
});
```


### Namespace chaining

Namespace is a special container that can be shared across constructor calls of `ap.App` and koa contexts.

To inject dependencies into namespace, `App.design` is used to create a app subclass and define its namespace.

To learn more about namespace, visit [othiym23/node-continuation-local-storage](https://github.com/othiym23/node-continuation-local-storage).

```
var App = ap.design('myapp', function(ns) {
  ns.set('foo', 'bar');
  assert(ns.get('origin') === 'container');
});
var Container = ap.Container.design('mycontaier', function(ns) {
  ns.set('origin', 'container');
  this.mount('foo', App);
  this.use(function*() {
    this.body = this.ns.get('foo');
  });
});
```

* a new context is created in the constructor call of every `App`
* if an app is mounted onto a container, in the runtime, all middlewares of this app will be run in a context that are merged from both container and app itself.
* namespace will be re-created in every request session and will be deserted after response


More examples can be found in [test/container_test.coffee](test/container_test.coffee).


## API

```
var ap = require('koa-app-party');
```

### ap

* ap.design

  Alias for `ap.App.design`

* ap.extend

  Alais for `ap.App.extend`

* ap.App

  Atomic app unit

* ap.Container

  Container app. Useful for mounting multiple atomic apps.

### App = ap.App

* App.extend(configurator)

  Create a subclass from current App. A `configurator` function will be invoked during constructor call with app instance as current context and other arguments passed to the constructor.

* App.extend(props)

  A plain object can be passed to extend the prototype of subclass. `props.init` will be called during constructor call.
  And a reference to super method (if any), is available as `this.super`.

* App.design(name, designer)

  Like `App.extend`, but `designer` will be run with the namespace instance as a single argument. `designer` is intended to modify the namespace during the constructor call.

* App.create(options)

  Create a `App` instance with options.

### var app = new App(options)

* app.listen(port, callback)

  Startup http server at local port assigned by `port`, and run callback.

* app.acceptServer(server, callback) -> Promise

  Before the call of `server.listen()`, the server instance is passed to this method to do everything you need. Return a promise to continue. Defaults to return a `Promise.resolve(true)`.

* app.inject(injector)

  Run `injector` with `namespace` and `callback` before `server.listen`. Ensure `callback` is run, or the server will never listen.


### Container = ap.Container

Subclass of `App`. Following class methods work just like the one on `App`:

* Container.create
* Container.design
* Container.extend


### var container = new ap.Container()

* container.mount(prefix, app)

  Mount an app at location of `prefix`.

* container.design(info)

  `info` is an object describing the layout of this container, such as

  ```
  {
    '/app1': App1,
    '/app2': App2
    '/': '/app1'
  }
  ```

###  Subclassing notes

* `Container.prototype.acceptServer` is pre-defined. You should call this method if you are writing an override version of `acceptServer` on a container.
* Always run `extend` or `design` to create subclasses.


## Contributor

RobinQu

## License

MIT

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