shan
koa with Promise-based middleware
Installation
$ npm install shan
Usage
You may need babel-node
to run following code (for async arrow function).
const shan = const favicon = let app = app app app app
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 callnext(ctx)
. It will return aPromise<any>
result from next middleware.
// ctx: KoaContext// next: (KoaContext) => Promise<any> app // use async arrow function, may need help from babelapp // use coapp // use bluebirdapp
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.
app app
Logger Middleware
Print request message to console when it flows through.
app // it's equivalent to app
Router Middleware
Simple router with fluent interface.
app
Tracer Middleware
Profiling time spent within in one or multiple middleware.
app app app app app
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
.
app // it's equivalent to app
License
MIT