All Projects → demopark → Koa Docs Zh Cn

demopark / Koa Docs Zh Cn

Licence: gpl-2.0
Koa 文档的中文版本 , 更新至 v2.12.0 版本.

Labels

Projects that are alternatives of or similar to Koa Docs Zh Cn

Koa Rest Api Boilerplate
💯 Boilerplate for Node.js Koa RESTful API application with Docker, Swagger, Jest, CodeCov and CircleCI
Stars: ✭ 420 (-48.91%)
Mutual labels:  koa
Kov Blog
A blog platform built with koa,vue and mongoose. 使用 koa ,vue 和 mongo 搭建的博客页面和支持markdown语法的博客编写平台,自动保存草稿。博客地址:https://chuckliu.me
Stars: ✭ 635 (-22.75%)
Mutual labels:  koa
Daruk
a node.js web framework based on typescript
Stars: ✭ 728 (-11.44%)
Mutual labels:  koa
Create Graphql
Command-line utility to build production-ready servers with GraphQL.
Stars: ✭ 441 (-46.35%)
Mutual labels:  koa
Koa Helmet
Important security headers for koa
Stars: ✭ 595 (-27.62%)
Mutual labels:  koa
Lin Cms Koa
🌀使用Node.JS KOA构建的CMS开发框架
Stars: ✭ 649 (-21.05%)
Mutual labels:  koa
React Lego
React-lego : incrementally add more cool stuff to your react app
Stars: ✭ 417 (-49.27%)
Mutual labels:  koa
Koa Passport
Passport middleware for Koa
Stars: ✭ 748 (-9%)
Mutual labels:  koa
Cabin
🌲 Cabin is the best JavaScript and Node.js logging service and logging npm package
Stars: ✭ 622 (-24.33%)
Mutual labels:  koa
React Vue Koa
Vue,React,微信小程序,快应用,TS , Koa, JS一把梭
Stars: ✭ 710 (-13.63%)
Mutual labels:  koa
Graphql Pokemon
Get information of a Pokémon with GraphQL!
Stars: ✭ 441 (-46.35%)
Mutual labels:  koa
Koajs Design Note
《Koa.js 设计模式-学习笔记》已完结 😆
Stars: ✭ 520 (-36.74%)
Mutual labels:  koa
Koa Views
Template rendering middleware for koa (hbs, swig, pug, anything! ✨)
Stars: ✭ 682 (-17.03%)
Mutual labels:  koa
Koa Boiler
Koa 2.7 boilerplate for a production-ready Node.js app with async/await.
Stars: ✭ 426 (-48.18%)
Mutual labels:  koa
Vue Koa Demo
🔰A simple full stack demo(CSR & SSR & Docker Support) written by Vue2 & Koa2(Koa1 verson also completed)
Stars: ✭ 730 (-11.19%)
Mutual labels:  koa
Joi Router
Configurable, input and output validated routing for koa
Stars: ✭ 418 (-49.15%)
Mutual labels:  koa
Node Blog
🚀《Node.js从入门到上线》A blog build with Koa2.
Stars: ✭ 640 (-22.14%)
Mutual labels:  koa
Koa Graphql
Create a GraphQL HTTP server with Koa.
Stars: ✭ 787 (-4.26%)
Mutual labels:  koa
Node Typescript Koa Rest
REST API boilerplate using NodeJS and KOA2, typescript. Logging and JWT as middlewares. TypeORM with class-validator, SQL CRUD. Docker included. Swagger docs, actions CI and valuable README
Stars: ✭ 739 (-10.1%)
Mutual labels:  koa
Koa2 Api Scaffold
一个基于Koa2的轻量级RESTful API Server脚手架。
Stars: ✭ 694 (-15.57%)
Mutual labels:  koa
用于 nodejs 的 koa 中间件框架

gitter NPM version build status Test coverage OpenCollective Backers OpenCollective Sponsors PR's Welcome

此项目同步自 koajs / koa 项目中的 docs. 除特殊情况, 将保持每月一次的同步频率.

Koa 通过 node.js 实现了一个十分具有表现力的 HTTP 中间件框架,力求让 Web 应用开发和 API 使用更加地愉快。Koa 的中间件之间按照编码顺序在栈内依次执行,允许您执行操作并向下传递请求(downstream),之后过滤并逆序返回响应(upstream)。

几乎所有 HTTP 服务器通用的方法都被直接集成到 Koa 大约570行源码的代码库中。其中包括内容协商,节点不一致性的规范化,重定向等等操作。

Koa没有捆绑任何中间件。

安装

Koa 依赖 node v7.6.0 或 ES2015及更高版本和 async 方法支持.

$ npm install koa

Hello koa

const Koa = require('koa');
const app = new Koa();

// 响应
app.use(ctx => {
  ctx.body = 'Hello Koa';
});

app.listen(3000);

入门

中间件

Koa 是一个中间件框架,可以采用两种不同的方法来实现中间件:

  • async function
  • common function

以下是使用两种不同方法实现一个日志中间件的示例:

async functions (node v7.6+)

app.use(async (ctx, next) => {
  const start = Date.now();
  await next();
  const ms = Date.now() - start;
  console.log(`${ctx.method} ${ctx.url} - ${ms}ms`);
});

Common function

// 中间件通常带有两个参数 (ctx, next), ctx 是一个请求的上下文(context),
// next 是调用执行下游中间件的函数. 在代码执行完成后通过 then 方法返回一个 Promise.

app.use((ctx, next) => {
  const start = Date.now();
  return next().then(() => {
    const ms = Date.now() - start;
    console.log(`${ctx.method} ${ctx.url} - ${ms}ms`);
  });
});

Koa v1.x 中间件签名

中间件签名在 v1.x 和 v2.x 之间已经被更改. 旧的签名已经被弃用.

旧的签名中间件支持将在 v3 中删除

请参阅 迁移指南 获取有关从 v1.x 升级并使用 v2.x 中间件的更多信息。

上下文, 请求和响应

每个中间件都接收一个 Koa 的 Context 对象,该对象封装了一个传入的 http 消息,并对该消息进行了相应的响应。 ctx 通常用作上下文对象的参数名称。

app.use(async (ctx, next) => { await next(); });

Koa 提供了一个 Request 对象作为 Contextrequest 属性。 Koa的 Request 对象提供了用于处理 http 请求的方法,该请求委托给 node http 模块的IncomingMessage

这是一个检查请求客户端 xml 支持的示例。

app.use(async (ctx, next) => {
  ctx.assert(ctx.request.accepts('xml'), 406);
  // 相当于:
  // if (!ctx.request.accepts('xml')) ctx.throw(406);
  await next();
});

Koa提供了一个 Response 对象作为 Contextresponse 属性。 Koa的 Response 对象提供了用于处理 http 响应的方法,该响应委托给 ServerResponse

Koa 对 Node 的请求和响应对象进行委托而不是扩展它们。这种模式提供了更清晰的接口,并减少了不同中间件与 Node 本身之间的冲突,并为流处理提供了更好的支持。 IncomingMessage 仍然可以作为 Context 上的 req 属性被直接访问,并且ServerResponse也可以作为Context 上的 res 属性被直接访问。

这里是一个使用 Koa 的 Response 对象将文件作为响应体流式传输的示例。

app.use(async (ctx, next) => {
  await next();
  ctx.response.type = 'xml';
  ctx.response.body = fs.createReadStream('really_large.xml');
});

Context 对象还提供了其 requestresponse 方法的快捷方式。在前面的例子中,可以使用 ctx.type 而不是 ctx.response.type,而 ctx.accepts 可以用来代替 ctx.request.accepts

关于 Request, ResponseContext 更多详细信息, 参阅 请求 API 参考, 响应 API 参考上下文 API 参考.

Koa 应用程序

在执行 new Koa() 时创建的对象被称为 Koa 应用对象。

应用对象是带有 node http 服务的 Koa 接口,它可以处理中间件的注册,将http请求分发到中间件,进行默认错误处理,以及对上下文,请求和响应对象进行配置。

了解有关应用程序对象的更多信息请到 应用 API 参考.

文档

Babel 配置

如果你正在使用的不是 node v7.6+, 我们推荐你用 @babel/preset-env 配置 babel :

$ npm install @babel/register @babel/preset-env @babel/cli --save-dev

在开发环境中, 你可能想要使用 @babel/register:

node --require @babel/register <your-entry-file>

在生产环境中, 你可能想要使用 @babel/cli 构建文件. 假设你正在编译 src 文件夹且想要输出 non-javascript 文件拷贝到新的 dist 文件夹中:

babel src --out-dir dist --copy-files

还有你的 .babelrc 配置:

{
  "presets": [
    ["@babel/preset-env", {
      "targets": {
        "node": true
      }
    }]
  ]
}

故障排除

在 Koa 指南中查阅 故障排除指南调试 Koa.

运行测试

$ npm test

赞赏支持

赞赏支持

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].