KOA入门

  |   Node.js Javascript

koa 是由 Express 原班人马打造的,致力于成为一个更小、更富有表现力、更健壮的 Web 框架。使用 koa 编写 web 应用,通过组合不同的 generator,可以免除重复繁琐的回调函数嵌套,并极大地提升错误处理的效率。koa 不在内核方法中绑定任何中间件,它仅仅提供了一个轻量优雅的函数库,使得编写 Web 应用变得得心应手。


我们开始创建一个目录hello-koa

mkdir hello-koa

再进入hello-koa文件

cd hello-koa

安装

npm install -g n
usr/local/bin/n -> /usr/local/lib/node_modules/n/bin/n
n@2.1.4 /usr/local/lib/node_modules/n

我们创建app.js,以下输入代码:

// 导入koa,和koa 1.x不同,在koa2中,我们导入的是一个class,因此用大写的Koa表示:
const Koa = require('koa');

// 创建一个Koa对象表示web app本身:
const app = new Koa();

// 对于任何请求,app将调用该异步函数处理请求:
app.use(async (ctx, next) => {
    await next();
    ctx.response.type = 'text/html';
    ctx.response.body = '<h1>Hello, koa2!</h1>';
});

// 在端口3000监听:
app.listen(3000);
console.log('app started at port 3000...');

第一种方法:
npm命令直接安装koa

npm install koa@2.0.0
oa@2.0.0 ../../node_modules/koa
├── escape-html@1.0.3
├── destroy@1.0.4
├── error-inject@1.0.0
├── content-type@1.0.2
├── content-disposition@0.5.1
├── parseurl@1.3.1
├── fresh@0.3.0
├── vary@1.1.0
├── statuses@1.3.1
├── koa-is-json@1.0.0
├── only@0.0.2
├── delegates@1.0.0
├── depd@1.1.0
├── is-generator-function@1.0.3
├── http-errors@1.5.1 (setprototypeof@1.0.2, inherits@2.0.3)
├── type-is@1.6.14 (media-typer@0.3.0)
├── on-finished@2.3.0 (ee-first@1.1.1)
├── mime-types@2.1.13 (mime-db@1.25.0)
├── debug@2.3.3 (ms@0.7.2)
├── accepts@1.3.3 (negotiator@0.6.1)
├── koa-compose@3.2.1 (any-promise@1.3.0)
├── koa-convert@1.2.0 (co@4.6.0)
├── http-assert@1.2.0 (deep-equal@1.0.1, http-errors@1.4.0)
└── cookies@0.6.2 (keygrip@1.0.1)

第二种方法:在hello-koa这个目录下创建一个package.json

{
    "name": "hello-koa2",
    "version": "1.0.0",
    "description": "Hello Koa 2 example with async",
    "main": "start.js",
    "scripts": {
        "start": "node start.js"
    },
    "keywords": [
        "koa",
        "async"
    ],
    "author": "Michael Liao",
    "license": "Apache-2.0",
    "repository": {
        "type": "git",
        "url": "https://github.com/michaelliao/learn-javascript.git"
    },
    "dependencies": {
        "babel-core": "6.13.2",
        "babel-polyfill": "6.13.0",
        "babel-preset-es2015-node6": "0.3.0",
        "babel-preset-stage-3": "6.5.0",
        "koa": "2.0.0"
    }
}

安装组件

npm install
koa@2.0.0 node_modules/koa
├── error-inject@1.0.0
├── escape-html@1.0.3
├── destroy@1.0.4
├── koa-is-json@1.0.0
├── content-type@1.0.2
├── vary@1.1.0
├── parseurl@1.3.1
├── content-disposition@0.5.1
├── fresh@0.3.0
├── only@0.0.2
├── statuses@1.3.1
├── delegates@1.0.0
├── depd@1.1.0
├── is-generator-function@1.0.3
├── on-finished@2.3.0 (ee-first@1.1.1)
├── type-is@1.6.14 (media-typer@0.3.0)
├── http-errors@1.5.1 (setprototypeof@1.0.2, inherits@2.0.3)
├── cookies@0.6.2 (keygrip@1.0.1)
├── koa-convert@1.2.0 (co@4.6.0)
├── mime-types@2.1.13 (mime-db@1.25.0)
├── accepts@1.3.3 (negotiator@0.6.1)
├── debug@2.3.3 (ms@0.7.2)
├── http-assert@1.2.0 (deep-equal@1.0.1, http-errors@1.4.0)
└── koa-compose@3.2.1 (any-promise@1.3.0)

babel-polyfill@6.13.0 node_modules/babel-polyfill
├── regenerator-runtime@0.9.6
├── babel-runtime@6.18.0
└── core-js@2.4.1

babel-core@6.13.2 node_modules/babel-core
├── slash@1.0.0
├── babel-messages@6.8.0
├── babel-template@6.16.0
├── path-exists@1.0.0
├── shebang-regex@1.0.0
├── path-is-absolute@1.0.1
├── babel-helpers@6.16.0
├── private@0.1.6
├── babylon@6.14.1
├── convert-source-map@1.3.0
├── debug@2.3.3 (ms@0.7.2)
├── source-map@0.5.6
├── babel-types@6.19.0 (to-fast-properties@1.0.2, esutils@2.0.2)
├── minimatch@3.0.3 (brace-expansion@1.1.6)
├── babel-code-frame@6.16.0 (js-tokens@2.0.0, esutils@2.0.2, chalk@1.1.3)
├── babel-traverse@6.19.0 (globals@9.14.0, invariant@2.2.2)
├── babel-generator@6.19.0 (jsesc@1.3.0, detect-indent@4.0.0)
├── json5@0.4.0
├── lodash@4.17.2
├── babel-register@6.18.0 (source-map-support@0.4.6, home-or-tmp@2.0.0, babel-core@6.18.2, mkdirp@0.5.1, core-js@2.4.1)
└── babel-runtime@6.18.0 (regenerator-runtime@0.9.6, core-js@2.4.1)

babel-preset-stage-3@6.5.0 node_modules/babel-preset-stage-3
├── babel-plugin-transform-async-to-generator@6.16.0 (babel-plugin-syntax-async-functions@6.13.0, babel-helper-remap-async-to-generator@6.18.0, babel-runtime@6.18.0)
└── babel-plugin-transform-exponentiation-operator@6.8.0 (babel-plugin-syntax-exponentiation-operator@6.13.0, babel-helper-builder-binary-assignment-operator-visitor@6.18.0, babel-runtime@6.18.0)

babel-preset-es2015-node6@0.3.0 node_modules/babel-preset-es2015-node6
├── babel-plugin-transform-es2015-destructuring@6.19.0 (babel-runtime@6.18.0)
├── babel-plugin-transform-es2015-parameters@6.18.0 (babel-helper-get-function-arity@6.18.0, babel-helper-call-delegate@6.18.0, babel-template@6.16.0, babel-types@6.19.0, babel-traverse@6.19.0, babel-runtime@6.18.0)
├── babel-plugin-transform-es2015-function-name@6.9.0 (babel-types@6.19.0, babel-helper-function-name@6.18.0, babel-runtime@6.18.0)
└── babel-plugin-transform-es2015-modules-commonjs@6.18.0 (babel-plugin-transform-strict-mode@6.18.0, babel-template@6.16.0, babel-types@6.19.0, babel-runtime@6.18.0)

运行

//加上harmony,才能支持ES6语法
node --harmony app.js

突然报错信息:

app.use(async (ctx, next) => {
              ^

SyntaxError: Unexpected token (
    at exports.runInThisContext (vm.js:53:16)
    at Module._compile (module.js:414:25)
    at Object.Module._extensions..js (module.js:442:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:311:12)
    at Function.Module.runMain (module.js:467:10)
    at startup (node.js:134:18)
    at node.js:961:3

这是因为Node.js只支持ES6,并不支持ES7,无法识别新的async语法。

要把ES7代码“转换”为ES6代码,可以用Babel实现

在hello-koa目录下新建

var register = require('babel-core/register');

register({
    presets: ['stage-3']
});

require('./app.js');

在package.json中添加依赖包

"dependencies": {
    "babel-core": "6.13.2",
    "babel-polyfill": "6.13.0",
    "babel-preset-es2015-node6": "0.3.0",
    "babel-preset-stage-3": "6.5.0",
    "koa": "2.0.0"
}

使用npm install命令安装之后,调试控制台输出如下:

node --debug-brk=40645 --nolazy start.js 
Debugger listening on port 40645
node start
app started at port 3000...

参考地址:

http://www.liaoxuefeng.com/wiki/001434446689867b27157e896e74d51a89c25cc8b43bdb3000/001471087582981d6c0ea265bf241b59a04fa6f61d767f6000