# shan

> Shan web app framework

Latest version **0.0.5** (published 2015-09-13) · MIT license · 0 weekly downloads

## Install

```sh
npm install shan
pnpm add shan
yarn add shan
bun add shan
```

## 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.0.5 |
| Published | 2015-09-13 |
| First published | 2015-08-19 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 12 |
| Known vulnerabilities | 0 (+7 in 3 direct dependencies) |
| Install scripts | no |
| Author | gyson |
| Maintainers | gyson |

## Links

- npm: https://www.npmjs.com/package/shan
- npm.io page: https://npm.io/package/shan

## Dependencies (12)

- [co](https://npm.io/package/co.md) ^4.6.0
- [ms](https://npm.io/package/ms.md) ^0.7.1
- [qs](https://npm.io/package/qs.md) ^4.0.0
- [koa](https://npm.io/package/koa.md) ^1.0.0
- [methods](https://npm.io/package/methods.md) ^1.1.1
- [statuses](https://npm.io/package/statuses.md) ^1.2.1
- [mime-types](https://npm.io/package/mime-types.md) ^2.1.5
- [koa-is-json](https://npm.io/package/koa-is-json.md) ^1.0.0
- [on-finished](https://npm.io/package/on-finished.md) ^2.3.0
- [compressible](https://npm.io/package/compressible.md) ^2.0.4
- [object-assign](https://npm.io/package/object-assign.md) ^4.0.1
- [path-to-regexp](https://npm.io/package/path-to-regexp.md) ^1.2.0

## Recent versions

- 0.0.5 (latest) — 2015-09-13
- 0.0.4 — 2015-09-13
- 0.0.3 — 2015-09-13
- 0.0.2 — 2015-09-12
- 0.0.1 — 2015-08-19

## README

# shan

koa with Promise-based middleware

## Installation

```
$ npm install shan
```

## Usage

You may need `babel-node` to run following code (for async arrow function).

```js
const shan = require('shan')
const favicon = require('koa-favicon')

let app = shan()

app.useKoa(favicon())

app.use(async (ctx, next) => {
    let start = Date.now()

    await next(ctx)

    ctx.response.set('X-Response-Time', (Date.now() - start) + 'ms')
})

app.use(ctx => {
    ctx.response.body = 'hello'
})

app.listen(4321)
```

## About

This is an application inherits from koa but override `app.use` to accept promise-based middleware instead of generator-based middleware.

## Promise-based Middleware

Promise-based middleware is a function with two arguments, `ctx` and `next`, and return a `any` or `Promise<any>`.
If `any` is returned, it will be wrapped to Promise using `Promise.resolve(value)`. If there is any exception, the
error will be wrapped to Promise using `Promise.reject(error)`.

* `ctx` is just instance of KoaContext.
* `next` contains info about next middleware. To invoke next middleware, just call `next(ctx)`. It will return a `Promise<any>` result from next middleware.

```js
// ctx: KoaContext
// next: (KoaContext) => Promise<any>

app.use((ctx, next) => {
    console.log(1)
    return next(ctx).then(() => {
        console.log(8)
    })
})

// use async arrow function, may need help from babel
app.use(async (ctx, next) => {
    console.log(2)
    await next(ctx)
    console.log(7)
})

// use co
app.use(co.wrap(function* (ctx, next) {
    console.log(3)
    yield next(ctx)
    console.log(6)
}))

// use bluebird
app.use(Bluebird.coroutine(function* (ctx, next) {
    console.log(4)
    yield next(ctx)
    console.log(5)
}))
```

## Utility Middleware

A few utility middleware are included for convenience.

#### Koa Converter Middleware

Convert koa generator-based middleware to promise-based middleware.

You can use any generator-based middleware from koa ecosystem.

```js
app.useKoa(require('koa-favicon')())

app.useKoa(function* (next) {
    // ...
    yield next
})
```

#### Logger Middleware

Print request message to console when it flows through.

```js
app.useLogger(ctx => `> ${ctx.method} ${ctx.path}`)

// it's equivalent to

app.use((ctx, next) => {
    console.log(`> ${ctx.method} ${ctx.path}`)
    return next(ctx)
})
```

#### Router Middleware

Simple router with fluent interface.

```js
app.useRouter(it => it
    .get('/', (ctx, next) => {

    })
    .post('/signup', (ctx, next) => {

    })
    .all('/repos/:id', (ctx, next) => {

    })
    .route('/users/:id', {
        get: (ctx, next) => {

        },
        post: (ctx, next) => {

        },
        delete: (ctx, next) => {

        }
    })
)
```

#### Tracer Middleware

Profiling time spent within in one or multiple middleware.

```js
app.useTracer('name1')

app.use(async (ctx, next) => {
    await sleep(100)
    await next(ctx)
    await sleep(400)
})

app.useTracer('name2')

app.use(async (ctx, next) => {
    await sleep(200)
    await next(ctx)
    await sleep(300)
})

app.useTracer('name3')
```

It will provide info like following:

```
name1 | +0           |     +403.419
name2 |   +102.017   |   +305.734  
name3 |     +202.964 | +0.449     
```

#### Try Middleware

Shortcut for `try...catch...finally`.

```js
app.useTry({
    catch(ctx, err) {
        // catch block
    },
    finally(ctx) {
        // finally block
    }
})

// it's equivalent to

app.use(async (ctx, next) => {
    try {
        await next(ctx)
    }
    catch (err) {
        // catch block
    }
    finally {
        // finally block
    }
})

```


## License

MIT

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