Skip to content

npm-koa #106

@mowatermelon

Description

@mowatermelon

知识点名称

官网

https://koajs.com/

image

官方仓库

https://github.com/koajs/koa

image

官方npm地址

依赖包地址

https://www.npmjs.com/package/koa

image

在线运行地址

https://npm.runkit.com/koa

基础介绍

Expressive HTTP middleware framework for node.js to make web applications and APIs more enjoyable to write. Koa's middleware stack flows in a stack-like manner, allowing you to perform actions downstream then filter and manipulate the response upstream.

Only methods that are common to nearly all HTTP servers are integrated directly into Koa's small ~570 SLOC codebase. This includes things like content negotiation, normalization of node inconsistencies, redirection, and a few others.

Koa is not bundled with any middleware.

Koa 是由Express团队设计的一个新的Web框架,其目的是为Web应用程序和API提供更小、更具表现力、更稳健的基础。

通过利用异步函数,Koa允许您放弃回调并大大提高错误处理能力。Koa没有在其核心中捆绑任何中间件,它提供了一套优雅的方法,使编写服务器变得快速而愉快。

Koa的中间件堆栈以类似堆栈的方式流动,允许您在下游执行操作,然后在上游过滤和操作响应。

只有几乎所有HTTP服务器都通用的方法才被直接集成到Koa的 570 sloc 小代码库中。这包括内容协商、节点不一致的规范化、重定向等。

基础安装

npm install koa --save   # npm
yarn add koa             # Yarn

官方文档

https://koajs.com/#Application

Koa应用程序是一个包含一系列中间件函数的对象,这些函数根据请求以堆栈方式组合和执行。Koa与您可能遇到的许多其他中间件系统类似,例如Ruby的Rack、Connect等等,但是我们做出了一个关键的设计决策,在其他低级中间件层提供高级“糖”。这提高了互操作性、健壮性,并使编写中间件变得更加愉快。

这包括用于诸如内容协商、缓存刷新、代理支持和重定向等常见任务的方法。尽管Koa提供了大量有用的方法,但是由于没有绑定中间件,Koa的占用空间很小。

image

https://github.com/koajs/workshop

image

基础使用

(function (log) {
const Koa = require('koa');
const app = new Koa();
 
// response
app.use(ctx => {
  ctx.body = 'Hello Koa';
});
 
app.listen(3000);
})(console.log)

其他网站

https://koa.bootcss.com/

image

Koa 应用程序是一个包含一组中间件函数的对象,它是按照类似堆栈的方式组织和执行的。 Koa 类似于你可能遇到过的许多其他中间件系统,例如 Ruby 的 Rack ,Connect 等,然而,一个关键的设计点是在其低级中间件层中提供高级“语法糖”。 这提高了互操作性,稳健性,并使书写中间件更加愉快。

这包括诸如内容协商,缓存清理,代理支持和重定向等常见任务的方法。 尽管提供了相当多的有用的方法 Koa 仍保持了一个很小的体积,因为没有捆绑中间件。

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions