Introduction
Koa is a new web framework designed by the team behind Express, +window.analytics.page();
Introduction
Koa is a new web framework designed by the team behind Express, which aims to be a smaller, more expressive, and more robust foundation -for web applications and APIs. Through leveraging generators Koa allows you +for web applications and APIs. By leveraging async functions, Koa allows you to ditch callbacks and greatly increase error-handling. Koa does not bundle any -middleware within core, and provides an elegant suite of methods that make +middleware within its core, and it provides an elegant suite of methods that make writing servers fast and enjoyable.
Installation
Koa requires node v7.6.0 or higher for ES2015 and async function support.
@@ -202,8 +202,8 @@Error Handling
snippet:app.use(async ctx => {
ctx; // is the Context
- ctx.request; // is a koa Request
- ctx.response; // is a koa Response
+ ctx.request; // is a Koa Request
+ ctx.response; // is a Koa Response
});
Many of the context's accessors and methods simply delegate to their ctx.request
or ctx.response
equivalents for convenience, and are otherwise identical. For example ctx.type
and ctx.length
delegate to the response
object, and ctx.path
and ctx.method
delegate to the request
.
ctx.res
res.end()
ctx.request
- A koa Request
object.
A Koa Request
object.
ctx.response
- A koa Response
object.
A Koa Response
object.
ctx.state
The recommended namespace for passing information through middleware and to your frontend views.
ctx.state.user = await User.find(id);
ctx.app
@@ -233,7 +233,7 @@ctx.cookies.get(name, [options])
signed
the cookie requested should be signed
koa uses the cookies module where options are simply passed.
+Koa uses the cookies module where options are simply passed.
ctx.cookies.set(name, value, [options])
Set cookie name
to value
with options
:
-
@@ -246,7 +246,7 @@
httpOnly
server-accessible cookie, true by defaultoverwrite
a boolean indicating whether to overwrite previously set cookies of the same name (false by default). If this is true, all cookies set during the same request with the same name (regardless of path or domain) are filtered out of the Set-Cookie header when setting this cookie.
ctx.cookies.set(name, value, [optio
koa uses the cookies module where options are simply passed.
+Koa uses the cookies module where options are simply passed.
ctx.throw([status], [msg], [properties])
Helper method to throw an error with a .status
property
defaulting to 500
that will allow Koa to respond appropriately.
@@ -263,12 +263,12 @@
ctx.throw([status], [msg], [properties error messages since you do not want to leak failure details.
You may optionally pass a properties
object which is merged into the error as-is, useful for decorating machine-friendly errors which are reported to the requester upstream.
ctx.throw(401, 'access_denied', { user: user });
koa uses http-errors to create errors.
+ctx.throw(401, 'access_denied', { user: user });
Koa uses http-errors to create errors.
ctx.assert(value, [status], [msg], [properties])
Helper method to throw an error similar to .throw()
when !value
. Similar to node's assert()
method.
ctx.assert(ctx.state.user, 401, 'User not found. Please login!');
koa uses http-assert for assertions.
+ctx.assert(ctx.state.user, 401, 'User not found. Please login!');
Koa uses http-assert for assertions.
ctx.respond
To bypass Koa's built-in response handling, you may explicitly set ctx.respond = false;
. Use this if you want to write to the raw res
object instead of letting Koa handle the response for you.
Note that using this is not supported by Koa. This may break intended functionality of Koa middleware and Koa itself. Using this property is considered a hack and is only a convenience to those wishing to use traditional fn(req, res)
functions and middleware within Koa.